当前位置:网站首页>2021-04-27 classes and objects

2021-04-27 classes and objects

2022-06-23 10:07:00 Deer like deer

The relationship between classes and objects

Class is an abstract data type , He is the overall description of a certain kind of things / Definition , But it can't represent a specific thing
Objects are concrete instances of abstract concepts

package oop.demo2;

// Students 
public class Student {
    
    // attribute : Field 
    String name;
    int age;

    // Method 
    public void study(){
    
        System.out.println(this.name+" I'm learning ");;
    }
}

package oop.demo2;

// There should be only one project main Method 
public class Application {
    
    public static void main(String[]args){
    
        // class : In the abstract , Instantiation 
        // Class will return its own object after instantiation 
        //student The object is a Student Class 
        Student xiaoming = new Student();
        Student xiaohong = new Student();

        xiaoming.name = " Xiao Ming ";
        xiaohong.age = 18;

        System.out.println(xiaoming.name);
        System.out.println(xiaohong.age);

    }
}

The essence of object-oriented programming is : Organize code as a class , The organization of objects ( encapsulation ) data

原网站

版权声明
本文为[Deer like deer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230954195782.html