Java object-oriented study notes-1

Preface

Java is an object-oriented programming language that can abstract and encapsulate real things through the concepts of classes and objects. This article will lead you to gradually understand the key points of Java object-oriented programming, and deepen your understanding through sample code. After studying this article, you will be able to skillfully use Java classes and objects to represent concepts in the real world, laying a solid foundation for subsequent Java learning.

classes and objects

A class is an abstraction of a group of objects with the same properties and behavior, and objects are instances of the class. We use four examples to understand the concepts of classes and objects:

Dog class

The Dog class is used to represent dogs. It includes attributes such as name, health value, intimacy, and behaviors such as eating food and printing information. We can create multiple Dog objects, each object represents a dog and has independent attribute values.

package cn.qq.xiangmu.entity;

public class Dog {
    
    
    private String name;
    private int health;
    private int love;
    private String strain;

    // 个人建议,没有什么特殊要求时,提供无参和满参构造函数即可

    // 默认系统会提供一个无参的公有的没有什么内容的构造函数
    public Dog() {
    
    
        //this("匿名", 50, 50, "土狗"); // 构造函数的调用,必须在函数体的第一行。不允许递归死循环调用。实际开发很少使用!
    }

    // 如果自定义了有参构造函数,系统就不再提供无参构造函数
    public Dog(String name, int health, int love, String strain) {
    
    
        //this(); // 构造函数的调用,必须在函数体的第一行。不允许递归死循环调用。实际开发很少使用!
        this.name = name;
        this.health = health;
        this.love = love;
        this.strain = strain;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getHealth() {
    
    
        return health;
    }

    public void setHealth(int health) {
    
    
        if (health < 0) health = 0;
        if (health > 100) health = 100;
        this.health = health;
    }

    public int getLove() {
    
    
        return love;
    }

    public void setLove(int love) {
    
    
        this.love = love;
    }

    public String getStrain() {
    
    
        return strain;
    }

    public void setStrain(String strain) {
    
    
        this.strain = strain;
    }

    public void print() {
    
    
        System.out.println("宠物的自白");
        System.out.println("我的名字叫"+name+",健康值是"+health+",和主人的亲密度是"+love+",我是一只"+strain);
    }
}

  • DogA class represents a dog and includes attributes such as name, health, intimacy, and breed.
  • Through the constructor, these properties can be initialized.
  • Getter and setter methods are provided to access and modify these properties.
  • printMethod is used to print the dog's information.

Penguin class

The Penguin class is used to represent penguins. Its attributes and behaviors are similar to those of the Dog class. The difference is that an attribute indicating gender is added. Multiple Penguin objects can be used to represent different penguins.

package cn.qq.xiangmu.entity;

public class Penguin {
    
    
    // 常量的定义 可以参考 Integer.MAX_VALUE
    public static final String SEX_MALE = "Q仔";
    public static final String SEX_FEMALE = "Q妹";
    private String name;
    private int health;
    private int love;
    private String sex;

    public Penguin() {
    
    
    }

    public Penguin(String name, int health, int love, String sex) {
    
    
        this.name = name;
        this.health = health;
        this.love = love;
        this.sex = sex;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getHealth() {
    
    
        return health;
    }

    public void setHealth(int health) {
    
    
        if (health < 0) health = 0;
        if (health > 100) health = 100;
        this.health = health;
    }

    public int getLove() {
    
    
        return love;
    }

    public void setLove(int love) {
    
    
        this.love = love;
    }

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public void print() {
    
    
        System.out.println("宠物的自白");
        System.out.println("我的名字叫"+name+",健康值是"+health+",和主人的亲密度是"+love+",性别是"+sex);
    }
}

  • PenguinThe class represents a penguin and includes attributes such as name, health, intimacy, and gender.
  • Through the constructor, these properties can be initialized.
  • Getter and setter methods are provided to access and modify these properties.
  • printMethod used to print penguin information.
  • SEX_MALEThe constant fields and are defined in the class SEX_FEMALEto represent the gender of the penguin.

Xxx class

package cn.qq.xiangmu.entity;

public class Xxx {
    
    
    public int a1 = 1;
    public static int a2 = 2;

    public Xxx() {
    
    
        System.out.println("Xxx()构造函数");
    }

    public Xxx(int a1) {
    
    
        System.out.println("Xxx(int)构造函数");
        this.a1 = a1;
    }

    public void f1() {
    
    
        System.out.println("f1(): " + this.a1 + Xxx.a2);
        Xxx.f2();
    }

    public static void f2() {
    
    
        System.out.println("f2(): " + Xxx.a2);
    }
}

  • XxxThe class contains an instance variable a1and a static variable a2.
  • Constructors create Xxxinstances of a class and initialize instance variables.
  • f1Methods are instance methods used to print the values ​​of instance variables a1and static variables a2.
  • f2Method is a static method and is used to print a2the value of a static variable.

Yyy class

package cn.qq.xiangmu.entity;

public class Yyy {
    
    
    int a1;
    public static int a2;

    public Yyy() {
    
    
        a1 = 10;
    }

    {
    
    
        // 代码块 构造了n次对象前,默认就会自动调用n次
        a1++;
    }

    static {
    
    
        // 静态代码块 该类有访问就唯一调用一次
        a2++;
    }

    public void f1() {
    
    
        System.out.println(a1);
    }
}

  • YyyThe class contains an instance variable a1and a static variable a2.
  • The constructor initializes instance variables a1.
  • Classes include instance initialization blocks that are automatically executed before the object is created.
  • Classes also include static initialization blocks, which are executed only once when the class is loaded.
  • f1Method is used to print a1the value of the instance variable.

Test class

Demo01 class

package cn.qq.xiangmu.test;

import cn.qq.xiangmu.entity.*;

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        Dog dog = new Dog("欧欧", 100, 0, "德国牧羊犬");
        System.out.println(Penguin.SEX_FEMALE);
        Penguin penguin = new Penguin("美美", 90, 10, Penguin.SEX_FEMALE);
        dog.print();
        penguin.print();
    }
}

  • Contains a mainmethod that demonstrates how to use Dogthe and Penguinclasses to create instances and print information about them.

Demo02 class

package cn.qq.xiangmu.test;

import cn.qq.xiangmu.entity.Xxx;

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        Xxx xxx1 = new Xxx(1111);
        Xxx xxx2 = new Xxx(11111);
        System.out.println(xxx1.a1);
        System.out.println(xxx2.a1);
        xxx1.f1();
        xxx2.f1();
        Xxx.a2 = 22;
        Xxx.f2();
    }
}

  • Contains a mainmethod that demonstrates how to use Xxxa class to create instances, call methods, and modify static variables.

Demo03 class

package cn.qq.xiangmu.test;

import cn.qq.xiangmu.entity.Yyy;

public class Demo03 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Yyy.a2);
        System.out.println(Yyy.a2);
    }
}

  • Contains a mainmethod that demonstrates how to use Yyya class to access its static variables a2.

Summarize

Through this study note, we have a comprehensive understanding of the various components of Java classes, including:

  • The difference between instance variables and static variables
  • The role of constructor
  • The difference between instance methods and static methods
  • Execution timing of instance initialization block and static initialization block

And the use of these concepts in actual programming is verified through test classes. This laid a solid foundation for our subsequent study of Java's object-oriented features such as inheritance, polymorphism, etc.

Guess you like

Origin blog.csdn.net/qq_42531954/article/details/132747279