102 lines
2.9 KiB
C#
102 lines
2.9 KiB
C#
|
|
using System.Runtime.InteropServices;
|
||
|
|
|
||
|
|
namespace Lesson4_封装_构造函数和析构函数练习
|
||
|
|
{
|
||
|
|
class Person
|
||
|
|
{
|
||
|
|
public string name;
|
||
|
|
public int age;
|
||
|
|
public Person(string name, int age)
|
||
|
|
{
|
||
|
|
this.name = name;
|
||
|
|
this.age = age;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Clas
|
||
|
|
{
|
||
|
|
public string name, major;
|
||
|
|
Person[] student;
|
||
|
|
public Clas(string name, string major, Person[] student)
|
||
|
|
{
|
||
|
|
this.name = name;
|
||
|
|
this.major = major;
|
||
|
|
this.student = student;
|
||
|
|
}
|
||
|
|
public void PrintInfo(Person[] student)
|
||
|
|
{
|
||
|
|
Console.WriteLine("{0}班级中学生信息",name);
|
||
|
|
for (int i = 0; i < student.Length; i++)
|
||
|
|
{
|
||
|
|
Console.WriteLine("student的姓名:" + student[i].name + "student的年龄:" + student[i].age);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Ticket
|
||
|
|
{
|
||
|
|
public uint distant;
|
||
|
|
|
||
|
|
public Ticket()
|
||
|
|
{
|
||
|
|
distant = 0;
|
||
|
|
}
|
||
|
|
public Ticket(uint distant)
|
||
|
|
{
|
||
|
|
this.distant = distant;
|
||
|
|
}
|
||
|
|
public void GetDistant()
|
||
|
|
{
|
||
|
|
Console.WriteLine(distant);
|
||
|
|
}
|
||
|
|
public void GetPrice()
|
||
|
|
{
|
||
|
|
double price = 0d;
|
||
|
|
if (distant <= 100)
|
||
|
|
{
|
||
|
|
price = distant;
|
||
|
|
}
|
||
|
|
else if (distant <= 200)
|
||
|
|
{
|
||
|
|
price = Convert.ToDouble(distant * 0.95);
|
||
|
|
}
|
||
|
|
else if (distant <= 300)
|
||
|
|
{
|
||
|
|
price = Convert.ToDouble(distant * 0.9);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
price = Convert.ToDouble(distant * 0.8);
|
||
|
|
}
|
||
|
|
Console.WriteLine(price);
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
internal class Program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
Person p1 = new Person("张三", 3);
|
||
|
|
Person p2 = new Person("李四", 4);
|
||
|
|
|
||
|
|
Console.WriteLine("P1姓名:" + p1.name + "P1年龄" + p1.age);
|
||
|
|
Console.WriteLine("P2姓名:" + p2.name + "P2年龄" + p2.age);
|
||
|
|
Person[] student = { p1, p2 };
|
||
|
|
Console.WriteLine("把P1和P2放到Person的student之后");
|
||
|
|
Console.WriteLine("student[0]的姓名:"+ student[0].name+ "student[0]的年龄:" + student[0].age);
|
||
|
|
Console.WriteLine("student[1]的姓名:" + student[1].name + "student[1]的年龄:" + student[1].age);
|
||
|
|
|
||
|
|
|
||
|
|
Clas c1 = new Clas("三年一班","计算机",student);
|
||
|
|
c1.PrintInfo(student);
|
||
|
|
|
||
|
|
Ticket t = new Ticket();
|
||
|
|
Console.Write("默认情况下的距离:");
|
||
|
|
t.GetDistant();
|
||
|
|
Console.Write("赋值初始化后的距离:");
|
||
|
|
Ticket t2 = new Ticket(300);
|
||
|
|
t2.GetDistant();
|
||
|
|
Console.Write("赋值初始化后的价格计算:");
|
||
|
|
t2.GetPrice();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|