03继承

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("我是一名记者");
        }
            

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值