当前位置:网站首页>Class C design questions
Class C design questions
2022-07-07 23:35:00 【Bobo in summer】
class Worker// Design staff (Worker) And its subclasses manager class (Manager), The Employee class contains private fields name,salary;
// And set its properties Name,Salary; The manager class also has its own private member field bonus, And its corresponding attributes Bonus;
{
string name;
public string Name
{
get {
return name; }
set {
name = value; }
}
int salary;
public int Salary
{
get {
return salary; }
set {
salary = value; }
}
public Worker(){
}// The employee class 、 Managers should have their own non participation 、 Parametric construction method ;
public Worker(string name, int salary)
{
this.name = name;
this.salary = salary;
}
}
class Manager:Worker
{
int bonus;
public int Bonus
{
get {
return bonus; }
set {
bonus = value; }
}
public Manager() {
}
public Manager(string name, int salary, int bonus)
: base(name, salary)
{
this.bonus = bonus;
}
}
// stay main Create an employee array in ( Managers as an element ), And assign values to each element of the array ,
// It is required to print out the name and salary information of the employee array .
Worker[] wkLD = new Worker[2];// Create an employee array
Worker wkWang = new Worker(" Xiao Wang ", 4000);// To be honest, the employee object is Xiao Wang
Manager mnLi = new Manager(" Manager Li ",10000,3000);
wkLD[0] = wkWang;// Employee Wang is the first element of the array
wkLD[1] = mnLi;
for (int i = 0; i < wkLD.Length; i++) // Cycle out names and salaries
{
Console.WriteLine(" full name :{0}, salary :{1},",wkLD[i].Name,wkLD[i].Salary);
}
class Student
{
string name;
public string Name
{
get {
return name; }
set {
name = value; }
}
int credit;
public int Credit
{
get {
return credit; }
set {
credit = value; }
}
public Student() {
}
public Student(string name, int credit)
{
this.name = name;
this.credit = credit;
}
}
class Graduate : Student
{
int postCredit;
public int PostCredit
{
get {
return postCredit; }
set {
postCredit = value; }
}
public Graduate() {
}
public Graduate(string name, int credit, int postCredit) : base(name, credit)
{
this.postCredit = postCredit;
}
}
Student[] stld = new Student[2];
Student st1 = new Student(" Xia Bo ",90);
Graduate st2= new Graduate(" satisfied ",80,50);
stld[0] = st1;
stld[1] = st2;
Console.WriteLine(st2.PostCredit);
for (int i = 0; i < stld.Length; i++)
{
Console.WriteLine(" full name {0}, credits {1}",stld[i].Name,stld[i].Credit);
}
class Vehicles // Define a name Vehicles The basic class of vehicles :
{
string brand, color;
public string Color
{
get {
return color; }
set {
color = value; }
}
public string Brand
{
get {
return brand; }
set {
brand = value; }
}
public Vehicles() {
}// Complete the parameterless and parameterized construction method of the base class , Parameter free constructor , If a class has a residual constructor defined , Then the system does not provide a parameterless constructor , Need to be manually added
public Vehicles(string brand, string color)
{
this.brand = brand;
this.color = color;
} // This class contains the private member field trademark and color , And set its corresponding public attributes ;
public virtual void run()
{
Console.WriteLine(" I've started .");
} // Class contains member methods Run To simulate the start of transportation , This method only outputs “ I've started ” Information ;
public void showInfo()
{
Console.WriteLine(" trademark :{0}, Color :{1}", this.brand, this.color);
} // Class contains member methods ShowInfo To display information , This method outputs the display trademark and color ;
}
class Car : Vehicles // To write Car Cars are inherited from Vehicles class , For such :
{
int seat;
public int Seat
{
get {
return seat; }
set {
seat = value; }
}// Add member field seats , And set its corresponding public attributes ;
public Car() {
} // Complete the nonparametric parametric constructor of car class
public Car(string brand, string color, int seat)
: base(brand, color)
{
this.seat = seat;
}
public void showCar()
{
Console.WriteLine(" Car brand :{0}, Color :{1}, Number of seats :{2}", this.Brand, this.Color, this.seat);
} // Add member method ShowCar, The output shows the information of the car ;
public override void run()
{
Console.WriteLine(" The car started ");
} // Covering the parent class Run Method , The output shows “ Information that the car has started ”;
}
static void Main(string[] args)
{
Vehicles v1 = new Vehicles(" ferrari ", " white ");
Car c1 = new Car(" Koniseg ", " violet ", 5);
c1.showCar();
v1.showInfo();
c1.run();
v1.run();
Console.ReadKey();
}
class Vehicles
{
string brand, color;
public string Color
{
get {
return color; }
set {
color = value; }
}
public string Brand
{
get {
return brand; }
set {
brand = value; }
}
public Vehicles() {
}
public Vehicles(string brand, string color)
{
this.brand = brand;
this.color = color;
}
public virtual void run()
{
Console.WriteLine(" I've started .");
}
public void showInfo()
{
Console.WriteLine(" trademark :{0}, Color :{1}", this.brand, this.color);
}
}
class Truck : Vehicles
{
int load;
public int Load
{
get {
return load; }
set {
load = value; }
}
public Truck() {
}
public Truck(string brand, string color, int load)
: base(brand, color)
{
this.load = load;
}
public void showTrunk()
{
Console.WriteLine(" Car brand :{0}, Color :{1}, load :{2}", this.Brand, this.Color, this.load);
}
public override void run()
{
Console.WriteLine(" The car started ");
}
}
Vehicles v1 = new Vehicles(" The public ", " white ");
Truck c1 = new Truck(" zhongtai ", " black ", 5000);
c1.showTrunk();
v1.showInfo();
c1.run();
v1.run();
Console.ReadKey();
interface IVehicle// Create a name IVehicle The interface of : Add two methods to the interface Start() and Stop() Used to describe the start and stop of the vehicle .
{
void start();
void stop();
}
class Bike:IVehicle// establish Bike Bicycles :
{
int wheel;// This class contains private member fields wheel Number of wheels , And set its corresponding public attributes ;
public int Wheel
{
get {
return wheel; }
set {
wheel = value; }
}
public Bike() {
}// Complete the parameterless and parametric construction method of this class ;
public Bike(int wheel)
{
this.wheel = wheel;
}
public void start()// Realization IVehicle Two methods of interface ;
{
Console.WriteLine(" I am a {0} A bicycle , I'm going to start ",this.wheel);
}
public void stop()
{
Console.WriteLine(" Bicycle station , brake ");
}
}
class Bus:IVehicle// establish Bus Bus class :
{
int seat;// This class contains private member fields seat Number of seats , And set its corresponding public attributes ;
public int Seat
{
get {
return seat; }
set {
seat = value; }
}
public Bus() {
}// Complete the parameterless and parametric construction method of this class ;
public Bus(int seat)
{
this.seat = seat;
}
public void start() // Realization IVehicle Two methods of interface ;
{
Console.WriteLine(" I am a {0} Seat car , I'm going to start ", this.seat);
}
public void stop()
{
Console.WriteLine(" The bus arrives at the station , brake ");
}
}
IVehicle[] iv = new IVehicle[2]; // stay main The method determines IVehicle Array , And store Bike,Bus Object to test the above
Bike bkBlack = new Bike(2);
Bus busWhilt = new Bus(24);
iv[0] = bkBlack;
iv[1] = busWhilt;
iv[0].start();
iv[0].stop();
iv[1].start();
iv[1].stop();
class Pet //Pet Define a pet class (Pet):
{
string name; // Define private member fields in this class name Name and age Age , And set its corresponding public attributes ;
public string Name
{
get {
return name; }
set {
name = value; }
}
int age;
public int Age
{
get {
return age; }
set {
age = value; }
}
public virtual void cry()// This class includes two methods : It's called Cry(), Eat something Eat();
{
Console.WriteLine(" I'm a pet , My name is {0}, I {1} Year old ", this.name, this.age);
}
public virtual void eat()
{
Console.WriteLine(" I'm a favorite pet ");
}
public Pet() {
} // Complete the parameterless and parametric construction method of this class ;
public Pet(string name, int age)
{
this.name = name;
this.age = age;
}
}
class Dog : Pet// Define the subclass of a pet dog (Dog):
{
public override void cry() // Covering the parent class Cry(),Eat() Method ; Add ways to watch the door GuardEntrance()
{
Console.WriteLine(" I'm a dog , My name is {0}, I {1} Year old , My name is Wangwang ", this.Name, this.Age);
}
public override void eat()
{
Console.WriteLine(" I like eating bones ");
}
public void GuardEntrance()
{
Console.WriteLine(" I watch the door for my master ");
}
public Dog() {
} // Complete the parameterless and parametric construction method of this class ;
public Dog(string name, int age) : base(name, age) {
}
}
class Cat : Pet // Define the subclass of pet cats (Cat):
{
public override void cry() // Covering the parent class Cry(),Eat() Method ;
{
Console.WriteLine(" I'm a cat , My name is {0}, I {1} Year old , My name is Miaomiao ", this.Name, this.Age);
}
public override void eat()
{
Console.WriteLine(" I like fish ");
}
public void HuntMice()// Increase the cat's own unique method of catching mice HuntMice();
{
Console.WriteLine(" I like catching mice ");
}
public Cat() {
}// Complete the parameterless and parameterized construction of this class
public Cat(string name, int age) : base(name, age) {
}
}
Pet pet1 = new Dog(" Xiao Huang ", 3);
pet1.cry();
pet1.eat();
((Dog)pet1).GuardEntrance();
Pet pet2 = new Cat(" The small white ", 2);
pet2.cry();
pet2.eat();
((Cat)pet2).HuntMice();
边栏推荐
- Understand TCP's three handshakes and four waves with love
- Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
- windows设置redis开启自动启动
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- Explain
- The for loop realizes 1-100 addition and eliminates the 4-digit tail number
- 高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
- B_QuRT_User_Guide(36)
- 2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
- SAP HR 家庭成员信息
猜你喜欢
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
JS get the key and value of the object
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
LeeCode -- 6. Z 字形变换
RE1 attack and defense world reverse
List. How to achieve ascending and descending sort() 2020.8.6
随机推荐
Markdown
Turbo introder common scripts
In the field of software engineering, we have been doing scientific research for ten years!
8.31 Tencent interview
谷歌浏览器怎么登录及开启同步功能
Three questions TDM
LDO穩壓芯片-內部框圖及選型參數
Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
LeeCode -- 6. Zigzag transformation
Mysql索引优化实战一
C simple question 2
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
MySQL架构
re1攻防世界逆向
C method question 1
欢聚时代一面
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
Entity层、DAO层、Service层、Controller层 先后顺序
How to change the formula picture in the paper directly into the formula in word
【7.4】25. K 个一组翻转链表