using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03继承
{
class Program
{
static void Main(string[] args)
{
//记者:我是记者 我的爱好是偷拍 我的年龄是34 我是男生
//司机:我是司机 我的年龄是43 我是男人 我的驾龄是23
//程序员:我是程序员 我的年龄是23 我是男生 我的工作年限是3
Driver dr = new Driver("司机", 43, "男", 23);
dr.DriverMethod();
Programmer pr = new Programmer("程序员", 23, "男", 3);
pr.ProgrammerMethod();
Reporter re = new Reporter("记者", 34, "男", "偷拍");
re.ReporterMethod();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03继承
{
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}
public Person(string name,int age,string sex)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
}
public void Text()
{
Console.WriteLine("测试");
}
public void SayHello()
{
Console.WriteLine("我是父类");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03继承
{
public class Driver:Person
{
private int _old;
public int Old
{
get { return _old; }
set { _old = value; }
}
public void DriverMethod()
{
Console.WriteLine("我是{0},我的年龄是{1},我是{2}生,我的驾龄是{3}", this.Name, this.Age, this.Sex, this.Old);
}
public Driver(string name,int age,string sex,int old):base( name,age,sex)
{
this.Old = old;
}
public new void SayHello()//new的作用一个是创建对象,另一个是隐藏从父类那里继承过来的同名成员
{
Console.WriteLine("我是司机");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03继承
{
public class Programmer:Person
{
private int _nian;
public int Nian
{
get { return _nian; }
set { _nian = value; }
}
public void ProgrammerMethod()
{
Console.WriteLine("我是{0},我今年{1}岁了,我是{2}生,我的工龄是{3}", this.Name, this.Age, this.Sex, this.Nian);
}
public Programmer(string name,int age,string sex,int nian):base(name,age,sex)
{
this.Nian = nian;
}
public new void SayHello()//关键字new的两个作用:一个是创建一个对象,另一个是隐藏从父类继承过来具有同名的成员
{
Console.WriteLine("我是程序员");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03继承
{
public class Reporter:Person
{
private string _hobby;
public string Hobby
{
get { return _hobby; }
set { _hobby = value; }
}
public void ReporterMethod()
{
Console.WriteLine("我是{0},我今年{1}岁了,我是{2}生,我的爱好是{3}", this.Name, this.Age, this.Sex, this.Hobby);
}
public Reporter(string name,int age,string sex,string hobby):base(name,age,sex)
{
this.Hobby = hobby;
}
public new void SayHello()//new关键字的作用是:一个是创建对象,另一个是隐藏从父类继承的同名成员
{
Console.WriteLine("我是一名记者");
}
}
}
03继承
最新推荐文章于 2019-12-06 20:01:26 发布