冷殇之箫
2016-11-04 09:31
using System;
namespace student
{
public class Port
{
private string name;
public int age;
public int height;
private Gender sex = Gender.男;
public string Name
{
get{return this.name;}
set{this.name=value;}
}
public Port(string n,int h,int a,Gender Sex)
{
name=n;
height=h;
age=a;
sex=Sex;
}
static void Main()
{
Port p1=new Port("lisa",3,24,Gender.女);
Console.WriteLine("我的名字叫"+p1.name+"今年"+p1.age+"岁了,"+"身高"+p1.height+"cm!"+p1.sex);
}
}
}
sex是私有字段,要被访问的话,加上public Gender Sex{get{return sex} set{sex=value}}
把private Gender 改成public Gender
用C#实现封装
78129 学习 · 182 问题
相似问题
回答 3