当前位置:网站首页>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();
边栏推荐
- MATLAB signal processing [Q & A essays · 2]
- USB (XVIII) 2022-04-17
- 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."
- Force deduction solution summary 648 word replacement
- 三问TDM
- KeePass realizes automatic input of web pages
- Lm12 rolling heikin Ashi double K-line filter
- C simple question 2
- The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
- Windows set redis to start automatically
猜你喜欢
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Matlab SEIR infectious disease model prediction
B_QuRT_User_Guide(36)
Anxin vb01 offline voice module access intelligent curtain guidance
Extended tree (I) - graphic analysis and C language implementation
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
SAP HR 劳动合同信息 0016
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
Explain
Ros2 topic (03): the difference between ros1 and ros2 [01]
随机推荐
LDO穩壓芯片-內部框圖及選型參數
Interface
Freelink open source call center design idea
谷歌浏览器怎么登录及开启同步功能
sql 数据库执行问题
SAP HR 家庭成员信息
IDEA 2021.3. X cracking
USB (XVIII) 2022-04-17
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
Illegal behavior analysis 1
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
Count the top 10 films at the box office and save them in another file
Spark 离线开发框架设计与实现
Live-Server使用
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
Three questions TDM
Explain