The relationship between Java classes and objects-14 days study notes

The relationship between class and object

  • Class is an abstract data type, it is the overall description and definition of a certain thing, but it cannot represent a specific thing

    • Animals, plants, mobile phones, computers. . . .
    • Person class, Pet class, Car class, etc., are used to describe and define the characteristics and behaviors of a certain type of specific things.
  • Objects are concrete instances of abstract concepts

    • Zhang San is a concrete example of people, and Zhang Sanjia’s prosperous wealth is a concrete example of dogs.

    • It can reflect the characteristics and show the concrete realization of a certain function, rather than an abstract concept.

    Organize code in a class, and organize (encapsulate) data in an object

package com.oop.Demo02;
//学生类
public class Studen {
    //一个类只有一个属性和一个方法

    //属性 ;字段
    String name; //初始化null
    int age ;  //初始化0
    //方法
    public void study(){
        System.out.println(name+"在学习");
    }
}


package com.oop.Demo02;

//一个项目应该只有一个main方法
public class Application {
    //类:抽象化的,实例化的
    //类实例化后会返还一个自己的对象
    //student 对象就是一个studen类的具体实例
    public static void main(String[] args) {
        //调用Studen需要new一下它实例化
        Studen studen = new Studen(); //学生可能是小张也可能是小陈。

        Studen xiaoMing = new Studen();
        Studen xiaoHong = new Studen();
        //给xiaoming,xiaohong赋值
        xiaoHong.name = "小陈";
        xiaoHong.age = 18;
        System.out.println(xiaoHong.name);
        System.out.println(xiaoHong.age);

        xiaoMing.name = "小张";
        xiaoMing.age = 18;
        System.out.println(xiaoMing.name);
        System.out.println(xiaoMing.age);






    }
}

Create and initialize objects

  • Create an object using the new object keyword

  • When using the new keyword to create, in addition to allocating space, the created object will be initialized by default and the constructor in the class will be called

  • The constructor in the class is also called the constructor, which must be called when creating an object. And the constructor has the following two characteristics

    1. Must be the same as the name of the class
    2. There must be no return type, and void cannot be written
package com.oop.Demo02;

//java  ---class
public class Person {
    
    
    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    String name;
    int age;
    //1.使用new关键字的本意还是在调用构造器
    //2.用来初始化数值
    public Person(){
    
      //无参构造

    }
    //有参构造
    //有参构造;有了定义了有参构造,无参就必须显示定义
    public Person(String name){
    
    
        this.name = name;//this指String中的name
    }
    //alt+ insert 快捷键

    public Person(int age) {
    
    
        this.age = age;
    }

    public Person(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }
    /*
     public static void main(String[] args) {
        //new 实例化一个对象
        Person person = new Person("小陈",18);
        System.out.println(person.name+"今年"+person.age);
    }
    构造器:
       1.和类名相同
       2。没有返回值
       3.初始化对象的值
       4.定义了有参构造后想用无参构造必须显示一个无参的构造
       
       this.代表当前类的  = 之后代表构造器传回来的

     */
}

Constructor must be mastered

Guess you like

Origin blog.csdn.net/yibai_/article/details/114776983
Recommended