Chapter 6 class and object

Time:2021-8-22

1、 Note map

2、 Example code:

public class PrintCarStatus{
    public static void main(String[] args){
        int speed;   
        String color; 
        String name; 
        String direction; 
        speed = 200;      
        Color = "red";
        Name = "Ferrari";
        Direction = "Nurburgring south ring track";
        System. Out. Println ("the speed of the passing car is:" + speed ");
        System. Out. Println ("its color is:" + color ");
        System. Out. Println ("the name of the car is" + name);
        System. Out. Println ("the driving direction of the vehicle is" + direction);
    }
}
//Create a car class, or define a car class
public class Car{
    int speed;
    String name;
    String color;
    String direction;
}
public class UseCar {
    public static void main(String[] args) {
        Car carPassedBy;          // 1) Declare a variable of car class
        carPassedBy = new Car();  // 2) Create an entity of car class
        carPassedBy.speed = 60;   // 3) Assign values to the attributes of the car entity through carpassedby
        Carpassedby. Color = "red";
        Carpassedby. Name = "Ferrari";
        Carpassedby.direction = "Nurburgring south ring track";
        System. Out. Println ("speed of passing car:" + carpassedby. Speed)// 4) Output the value of each attribute of the car entity through carpassedby
        System. Out. Println ("its color is:" + carpassedby. Color);
        System. Out. Println ("name of car:" + carpassedby. Name);
        System. Out. Println ("the driving direction of the vehicle is:" + carpassedby. Direction);
    }
}
public class Plural{
    int realPart;    // Save the value of complex real part
    int virtualPart; // Saves the value of the imaginary part of a complex number
}
public class UsePlural{
    public static void main(String[] args){
        Plural a;            // Declare a plural a
        a = new Plural();    // Create a complex object
        a.realPart = 5;      // Assign a value to the realpart property of this complex object
        a.virtualPart = 10;  // Assign a value to the virtualpart property of this complex object
        System. Out. Println ("the real part of the complex number is" + a.realpart); 
        System. Out. Println ("imaginary part of complex number is" + a.virtualpart); 
    }
}
public class Driver{
    String name;  // The default value is null
    int age;  // The default value is 0
    Car car;  // The default value is null
}
public class PrintDriverProps{
    public static void main(String[] agrs){
        Driver driver = new Driver();
        /*System.out.println(driver.name); 
        System.out.println(driver.age); 
        System.out.println(driver.car); */
        System.out.println(driver.age);
        if (driver.name !=null){
            System. Out. Println ("driver's name is:" + driver. Name ");
        }else {
            System. Out. Println ("no driver's name");
        }
        if (driver.car !=null){
            System. Out. Println ("driver's information is:" + driver. Car ");
        }else {
            System. Out. Println ("no driver information");
        }
    }
}

 

public class UseMotorcade{
    public static void main(String[] args){
        Motorcade groupCar = null;
        groupCar = new Motorcade();
        Groupcar. Name = "one fleet";
        int carCount = 5;
        groupCar.cars = new Car[5];
        String direction = "Zhenjiang";
        for (int i = 0;i < carCount; i++){
            groupCar.cars[i] = new Car();
            Groupcar. Cars [i]. Name = "car" + (I + 1);
            groupCar.cars[i].direction = direction;
        }
        System. Out. Println ("name of Fleet:" + groupcar. Name);
        System. Out. Println ("the names of cars in the fleet are:" + groupcar. Name);
        for (int i = 0;i < carCount;i++){
            System.out.println(groupCar.cars[i].name);
        }
        
    }
}

 

3、 Exercises

 

public class UseBook{
    public static void main(String[] args){
        Book book = new Book();
        Book.bookname = "Java from getting started to giving up";
        book.pageCount = 641;
        book.price = 74.5;
        System. Out. Println ("the title of this book is:" + book. Bookname ");
        System. Out. Println ("total pages of this book:" + book. Pagecount);
        System. Out. Println ("the price of this book is:" + book. Price ");
    }
}