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