using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
{
class Program
{
static void Main(string[] args)
{
classA str = new classA();//实例化
Console.WriteLine("我的名字叫:{0}\n性别:{1}", str.name, str.sex);//可以访问到name,sex两个字段
classB stf = new classB();//通过继承访问height
Console.WriteLine("我的名字叫:{0}\n性别:{1},身高:{2}", str.name, str.sex,stf.Getheight());
Console.ReadKey();
}
}
public class classA
{
public string name = "小明";//定义一个public访问属性(访问不受限制访问不受限制)
private DateTime birthday = Convert.ToDateTime("1991-10-1");
//定义一个私有的访问属性(访问仅限于包含类型)
int age = 18;//不加修饰符默认表示私有
protected double height = 175d;//访问仅限于包含类或从包含类派生的类型
internal string sex = "男";//访问仅限于当前程序集
}
public class classB : classA//通过继承classA来访问protected中的height字段
{
public double Getheight()
{
return this.height;//返回值
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
{
class Program
{
static void Main(string[] args)
{
classA str = new classA();//实例化
Console.WriteLine("我的名字叫:{0}\n性别:{1}", str.name, str.sex);//可以访问到name,sex两个字段
classB stf = new classB();//通过继承访问height
Console.WriteLine("我的名字叫:{0}\n性别:{1},身高:{2}", str.name, str.sex,stf.Getheight());
Console.ReadKey();
}
}
public class classA
{
public string name = "小明";//定义一个public访问属性(访问不受限制访问不受限制)
private DateTime birthday = Convert.ToDateTime("1991-10-1");
//定义一个私有的访问属性(访问仅限于包含类型)
int age = 18;//不加修饰符默认表示私有
protected double height = 175d;//访问仅限于包含类或从包含类派生的类型
internal string sex = "男";//访问仅限于当前程序集
}
public class classB : classA//通过继承classA来访问protected中的height字段
{
public double Getheight()
{
return this.height;//返回值
}
}
}