C# Object-Oriented Programming Course Experiment 5: Experiment Name: C# Object-Oriented Technology

insert image description here

Experiment content: C# object-oriented technology

insert image description here

1. Experimental purpose and requirements

  • (1) Master the inheritance characteristics of classes;
  • (2) Learn to use C# to implement class inheritance;
  • (3) Understand the polymorphic characteristics of classes;
  • (4) Learn to use C# method rewriting;

2. Experimental environment

Microsoft Visual Studio 2008

3. Experimental content and steps

3.1. Experiment content: test class, realize polymorphism

Experimental content: test class, the content of polymorphism is as follows

Define a base class called Vehicles to represent vehicles. This class should contain the attribute Brand of string type to represent the trademark, and Color to represent the color. It should also include Run (driving, display "I have started" on the console) and virtual method ShowInfo (display information, display trademark and color on the console), and write a constructor to initialize its properties. Write the Car (car) class inherited from the Vehicles class, add the int type attribute Seats (seat), and rewrite the member method ShowInfo (display the information of the car on the console), and write the construction method. Write the Truck (truck) class inherited from the Vehicles class, increase the float type member attribute Load (overload), and rewrite the member method ShowInfo (display truck information on the console), and write the construction method. Test the above types in the Main method to achieve polymorphism.

3.2. Experimental steps

3.2.1. Experimental procedure

1. The experimental procedure is as follows

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

namespace 实验五_1_
{
    
    
    class Vehicles
    {
    
    
        private string Brand;            //字段
        private string Color;

        public Vehicles(string brand, string color)     //构造函数
        {
    
    
            Brand = brand;
            Color = color;
        }

        public Vehicles() 
        {
    
    
        }

        public void Run()
        {
    
    
            Console.WriteLine("我已经开动了");
        }

        public virtual void ShowInfo()   //虚方法
        {
    
    
            Console.WriteLine("商标: {0},颜色:  {1}", Brand, Color);
        }
    }

    class Car : Vehicles          
    {
    
    
        private int Seats;

        public Car(int seats)
        {
    
    
            Seats = seats;
        }

        public override void ShowInfo()        //重载虚方法
        {
    
    
            Console.WriteLine("座位: {0}", Seats);
        }
    }

    class Truck : Vehicles
    {
    
    
        private float Load;

        public Truck(float load)
        {
    
    
            Load = load;
        }

        public override void ShowInfo()
        {
    
    
            Console.WriteLine("载重: {0}顿", Load);
        }
    }

    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Vehicles car = new Vehicles("奔驰", "红色");
            Vehicles truck = new Vehicles("东风", "蓝色");
            Car carSeat = new Car(4);
            Truck truckLoad = new Truck(2);

            car.ShowInfo();
            truck.ShowInfo();
            carSeat.ShowInfo();
            truckLoad.ShowInfo();

            Console.ReadLine();
        }
    }
}

3.2.2. Experimental results

2. The results of the experiment are as follows:

insert image description here

3.3. Experiment content: Create a Vehicle class and declare it as an abstract class

Experiment content: Create a Vehicle class and declare it as an abstract class as shown below

Create a Vehicle class and declare it abstract. Declare a NumOfWheels method in the Vehicle class so that it returns a string value. Create two classes Car and Motorbike inherited from the Vehicle class and implement the NumOfWheels method in these two classes. In the Car class, the "Four Wheels" information should be displayed, and in the Motorbike class, the "Two Wheels" information should be displayed. Create instances of Car and Motorbike in the Main() method and display messages in the console.

3.4. Experimental steps

3.4.1. Experimental procedure

1. The experimental procedure is as follows:

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

namespace 实验五_2_
{
    
    
    abstract class Vehicle
    {
    
    
        public virtual void NumOfWheels()   //创建虚方法
        {
    
     
        }
    }

    class Car:Vehicle 
    {
    
    
        public override void NumOfWheels()   //重载虚方法,输出信息
        {
    
    
            Console.WriteLine("四轮车");
        }
    }

    class Motorbike : Vehicle
    {
    
    
        public override void NumOfWheels()
        {
    
    
            Console.WriteLine("双轮车");
        }
    }

    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Car car = new Car();                    //创建派生类的对象
            Motorbike motorbike = new Motorbike();
            car.NumOfWheels();                      //调用派生类的方法
            motorbike.NumOfWheels();
            Console.ReadLine();
        }
    }
}

3.4.2. Experimental results

2. The running effect of the experiment is as follows:
insert image description here
insert image description here

4. Experimental summary

  • 1. Through this experiment, I have mastered the use of C#'s derived classes, method rewriting, and method polymorphism.
  • 2. Master the inheritance characteristics of classes.
  • 3. Deepen the concept of inheriting the attributes of the base class for the derived class. In addition to the attributes of the base class, the derived class also has its own attributes.
  • 4. During the first experiment, the base class did not have a "0" parameter constructor.
    So I added the sentence
public Vehicles() 
{
    
    
}
  • Make the base class have a no-argument constructor.

insert image description here

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/127140789