C#继承(2)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PTA
{
    class Person
    {
        public string name;
        public int age;


        public virtual void sayHello()
        {
            Console.WriteLine("Hello!My name is " + name);
        }
        public virtual void sayHello(Person another)
        {
            Console.WriteLine("Hello," + another.name + "!My name is " + name);
        }


        public bool isOlderThan(int anAge)
        {
            bool flg;
            if (age > anAge)
                flg = true;
            else
                flg = false;
            return flg;
        }


        public Person(string n, int a)
        {
            name = n;
            age = a;
        }
        public Person(string n)
        {
            name = n;
            age = -1;
        }
        public Person() : this("", 0)
        {

        }

    }
    class Student : Person
    {
        public string school;
        public int score = 0;
        public bool isGoodStudent()
        {
            return score >= 90;
        }
        public override void sayHello()
        {
            base.sayHello();
            Console.WriteLine("My school is " + school);
        }
        public void sayHello(Student another)
        {
            Console.WriteLine("Hi!");
            if (school == another.school)
                Console.WriteLine("Schoolmates");
        }
        public void testThisSuper()
        {
            int a;
            a = age;
            a = this.age;
            a = base.age;
        }
        public Student()
        {

        }
        public Student(string name,int age,string school) : base(name, age)
        {
            this.school = school;
        }
        public static void Main(String[] argv)
        {
            Person p = new Person("Liming", 50);
            Student s = new Student("Wangqiang", 20, "PKU");
            Person p2 = new Student("Zhangyi", 18, "THU");
            Student s2 = (Student)p2;
            s2.sayHello();
        }
    }

}

输出结果:

Hello!My name is Zhangyi
My school is THU

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值