首页 / UNITY / unity第二周:关于属性
unity第二周:关于属性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了unity第二周:关于属性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4056字,纯文字阅读大概需要6分钟。
内容图文
一.
using System;
//属性,property
//可以在类,结构体,接口中
//属性本质是方法
//field:域,字段
//属性:不确定存储位置。set,get,访问器
namespace _013_属性
{
public class A
{
private int num;
public int Age
{
get { return num; }
set
{
num = value;//这个value的意思是外面传进来的值
}
}
public int Myage { get; set; }//两种等价,都是属性,简写更好
}
public class Student
{
private string code = "coder";
private string name = "name";
private int age;
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public override string ToString()
{
return "Code = "+Code+" ,Name = "+Name+" ,age= "+Age;
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Age = 21;
student.Name = "约翰.马可";
student.Code = "ProjectOne";
Console.WriteLine("StudentInfo: "+student.ToString());
}
}
}
二.
using System;
namespace _014_抽象属性
{
public abstract class Person
{
public abstract string Name
{
get;
set;
}
//快速敲出属性:prop+ tab键
public abstract int Age { get; set; }//这里实际上是没有实现的,只是因为是语法糖,看起来像实现的
//抽象属性不能像下面这么写,因为抽象说明不能实现,下面这么写实际上就是实现了
// private int age;
// public abstract int Age {
// get
// {
// return age;
// };
// set
// {
// age = value;
// };
// }
//
}
public class Student : Person
{
private string code = "code";
private string name = "name";
private int age = 0;
public override string Name
{
//lamda表达式,暂不解释
// get => throw new NotImplementedException();
// set => throw new NotImplementedException();
get
{
return name;
}
set
{
name = value;
}
}
public override int Age
{
// get => throw new NotImplementedException();
// set => throw new NotImplementedException();
get
{
return age;
}
set
{
age = value;
}
}
public string Code { get; set; }
public override string ToString()
{
return "Code = " + Code + " ,Name = " + Name + " ,age= " + Age;
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Age = 10;
student.Name = "约翰.马可";
student.Code = "ProjectOne";
student.Age++;
Console.WriteLine("StudentInfo: " + student.ToString());
}
}
}
内容总结
以上是互联网集市为您收集整理的unity第二周:关于属性全部内容,希望文章能够帮你解决unity第二周:关于属性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。