7. Introduction to Java [Object-Oriented]

Object-oriented is the core concept of Java programming. If you cannot fully understand the idea of ​​object-oriented, it will bring you a lot of troubles in business design during the actual project development process.

1. Constructor

After we design a class, when using this class to create an object instance, some scenarios require initialization of the instance, so Java provides a constructor to meet this requirement.

1. Principles

  • The name must be the same as the class name
  • Cannot have return value and return type
  • The constructor can also be overloaded (the parameter list is different)
  • When a class is designed, if no constructor is written, Java will automatically generate a parameterless constructor for the class.
  • If you define a parameterized constructor for a class, no parameterless constructor is created. [It is recommended to manually create a parameterless constructor]

2. Various forms of constructors

Default parameterless constructor:

class Rock{
    
    
	printme(){
    
    
		System.out.print("printme");
	}
}

Rock r=new Rock();

There are parametric constructors:

class Rock {
    
    
    Rock(String name) {
    
    
        System.out.print("这是一个有参构造器:" + name);
    }
}
Rock r = new Rock("IT飞牛");
//这是一个有参构造器:IT飞牛

Constructor overloading:

class Rock {
    
    
    Rock(String name) {
    
    
        System.out.println("这是一个有参构造器,姓名:" + name);
    }

    Rock(String name, int age) {
    
    
        this(name);//这里使用this调用其他构造器,复用
        System.out.println("这是一个有参构造器,年龄:" + age);
    }
}

Rock r = new Rock("IT飞牛", 18);

//这是一个有参构造器,姓名:IT飞牛
//这是一个有参构造器,年龄:18

Two, overload

A method in a class. In some scenarios, different operations need to be performed according to different situations. Of course, we can directly define different methods to achieve it. But sometimes it is indeed the same type of operation, and the method name should be the same. At this time, we can use overloading to achieve it.

The overloading in the class is mainly based on the list of parameter types passed in by the method to automatically identify and call different methods for execution.

The following provides examples with the same method name but different parameters:

package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        Rock r = new Rock();
        r.say();
        r.say("你好");
        r.say("你好", 3);
        r.say(3, "你好");
    }
}

class Rock {
    
    
    void say() {
    
    
        System.out.println("啥都没说");
    }

    void say(String msg) {
    
    
        System.out.println("说:" + msg);
    }

    void say(String msg, int num) {
    
    
        System.out.println(msg + ",被说了" + num + "遍");
    }

    void say(int num, String msg) {
    
    
        System.out.println("说了" + num + "遍," + msg);
    }
}
//啥都没说
//说:你好
//你好,被说了3遍
//说了3遍,你好

It can be seen that different input parameters call different methods with the same name.

Overloading involving basic types may cause deviations in the methods called due to variable type promotion. For example: Rock has added a domethod, calling r.do(1)this method, the parameters will intbe identified according to the type. Please see the following example:"

package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        Rock r = new Rock();
        r.say(2);
        r.say((byte) 2);
    }
}

class Rock {
    
    
    void say(byte num) {
    
    
        System.out.println("这是byte");
    }

    void say(int num) {
    
    
        System.out.println("这是int");
    }

}
//这是int
//这是byte

Three, this keyword

thisIt is a variable that can be used in a method to get the current object.

Friends who have been in contact with js should be better able to understand. The pointing of this in java and the pointing of this in js is a principle: [whoever calls, this points to whom]

Fourth, the meaning of static

staticA method is thisa method that does not exist, staticand cannot be called inside a method this. And staticnon-static methods cannot be called inside.

And you can call methods only through the class itself without creating any objects static. This is actually staticthe main purpose of a method, much like a global method.

Five, super

Access to parent class members can be achieved through the super keyword, which is used to refer to the parent class of the current object.

6. Packaging

reasonable concealment reasonable exposure

1. Access control

The method or attribute in the java class can set the access range, and the modifiers are: private, protect, public.

  • private: This member can be accessed by internal members of this class;

  • default: This member can be accessed by internal members of this class, and can also be accessed by other classes under the same package;

  • protected: This member can be accessed by internal members of the class, other classes in the same package, and its subclasses;

  • public: This member can be accessed by members of any class under any package.

2. Read and write interceptor getter, setter

javaThe read-write interceptor in is publicimplemented based on the method.

package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        Rock r = new Rock();
        r.setName("张三");
        System.out.println(r.getName());
        r.setAge(20);
        System.out.println(r.getAge());
    }
}

class Rock {
    
    
    private String name;

    public String getName() {
    
    
        return name;
    }

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

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    private int age;

}
//张三
//20

IDEA can be used to quickly generate getterand setting, as shown below:

image-20230725200155008.image-20230725200211337

7. Entity class

Features of entity classes:

  1. The member variables in this class must be private, and the corresponding getXxx and setXxx methods must be provided externally.
  2. There must be a public no-argument constructor in the class.

The following Studentclass is a typical entity class. The entire class is used to save student data without any other data processing business. Generally, the entity class will be used in conjunction with the operation class of the entity class. The entity class stores data, and the operation class StudentOperatoruses the data of the entity class for data processing business.

code show as below:

package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        Student std1 = new Student("张三", 60);
        StudentOperator sto1 = new StudentOperator(std1);
        sto1.printPass();

        Student std2 = new Student("李四", 50);
        StudentOperator sto2 = new StudentOperator(std2);
        sto2.printPass();
    }
}

class StudentOperator {
    
    
    private Student student;

    public StudentOperator(Student student) {
    
    
        this.student = student;
    }

    public void printPass() {
    
    
        if (student.getScore() >= 60) {
    
    
            System.out.println(student.getName() + ",及格了");
        } else {
    
    
            System.out.println(student.getName() + ",没及格");
        }
    }
}

class Student {
    
    
    private String name;
    private int score;

    public Student(String name, int score) {
    
    
        this.name = name;
        this.score = score;
    }

    public Student() {
    
    
    }

    public String getName() {
    
    
        return name;
    }

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

    public int getScore() {
    
    
        return score;
    }

    public void setScore(int score) {
    
    
        this.score = score;
    }
}

7. Inheritance

Inheritance is an important means to achieve software reuse.

The two most commonly used keywords in inheritance are extends and implements . By using these two keywords, we can achieve an object to get the properties of another object.

All Java classes are inherited from the java.lang.Object class, so Object is the ancestor class of all classes, and all classes must have a parent class except Object.

public interface Animal {
    
    }

public class Mammal implements Animal{
    
    
}

public class Dog extends Mammal{
    
    
}

Java does not support multiple inheritance. For example, the following writing is wrong:

public class extends Animal, Mammal{
    
    } 

But we can use the interface to achieve (multi-inheritance interface to achieve), the code structure is as follows:

//Apple.java
package com.itfeiniu.hello;

class Fruit {
    
    
    String color;
    int kg;
    String shape;
}

interface Fruit1 {
    
    
    public void eat1();

    public void travel1();
}

interface Fruit2 {
    
    
    public void eat2();

    public void travel2();
}

public class Apple extends Fruit implements Fruit1, Fruit2 {
    
    
    public Apple(String color, int kg, String shape) {
    
    
        this.color = color;
        this.kg = kg;
        this.shape = shape;
    }

    public void eat1() {
    
    
        System.out.println("eat1");
    }

    public void travel1() {
    
    
        System.out.println("travel1");
    }

    public void eat2() {
    
    
        System.out.println("eat2");
    }

    public void travel2() {
    
    
        System.out.println("travel2");
    }
}

//helloworld.java
package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        Apple app = new Apple("red", 5, "circle");
        app.eat1();
        app.eat2();
        System.out.println(app.color);
    }
}
//eat1
//eat2
//red

Guess you like

Origin blog.csdn.net/bobo789456123/article/details/131869088