C# Classes and Objects

October 20, 2022 640hotness 0likes 0comments

What is Classes in C#?

Objects

Access Modifiers

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers

https://www.cnblogs.com/xinaixia/p/5775471.html

Code

using System;
 //class declaration
public class Car
{
     //instance 6 variables
    String name;
    String color;
    String transmission;
    String bodytype;
    int seat;
    int year;
     //constructor declaration of class
    public Car(String name, String color, String transmission, String bodytype, int seat, int year)
    {
        this.name = name;
        this.color = color;
        this.transmission = transmission;
        this.bodytype = bodytype;
        this.seat = seat;
        this.year = year;
     }
     //property 6
    public String getName()
    {
        return name;
     }
    public String getColor()
    {
        return color;
     }
    public String getTransmission()
    {
        return transmission;
     }
    public String getType()
    {
        return bodytype;
     }
    public int getSeat()
    {
        return seat;
     }
    public int getYear()
    {
        return year;
     }
     //method 
    public String ToString()
    {
        return ("This is Car detail- CarName:" + this.getName() + "\n Color :" + this.getColor()
            + "\n Transmission: " + this.getTransmission() + "\n Body type: " + this.getType()
            + "\n Seat : " + this.getSeat() + "\n Year: " + this.getYear())+"\n =================";
     }
     //main method
    public static void Main(string[] args)
    {
        // 5 object
        Car Toyota = new Car("Toyota Rush", "Silver", "Auto", "SUV", 7, 2020);
        Car Bmw = new Car("BMX X1", "Silver", "Auto", "SUV", 7, 2022);
        Car Honda = new Car("Accord", "Black", "Auto", "Sedan", 4, 2016);
        Car Tesla = new Car("Tesla S", "Red", "Auto", "Sedan", 4, 2017);
        Car Mazda = new Car("Mazda 929 Legato", "White", "Manual", "Sedan", 4, 1980);
 
         //output 
        Console.WriteLine(Toyota.ToString());
         Console.WriteLine(Bmw.ToString());
         Console.WriteLine(Honda.ToString());
         Console.WriteLine(Tesla.ToString());
         Console.WriteLine(Mazda.ToString());
 
        Console.WriteLine("==============\n Thank you !");
        //out put date and time
         DateTime now = DateTime.Now;
        Console.WriteLine("Print date: " + now);
         Console.ReadLine();
    }
}

小四

喜欢摄影 和IT

Comments