Java Quick Start--4--Object Oriented

Java is almost the best object-oriented language, compared to C++, you can feel how concise the object-oriented part of java is.

Object Oriented Concepts

In the real world, whether we are humans, animals, or machines, we can call them an object. They all have a common feature. This feature is that they are a collection of attributes. Therefore, we put A collection with a bunch of properties is called an object. For example, a person can have a name, age, gender and other attributes. If we abstract it, it is an object. How to define an object in java?

public class Person{
    int age;
    String name;
    char sex;
}

This constructs a Person object, for example, we define it as, Mr. Zhuge.

    public static void main(String[] args) {
        Person person = new Person();
        person.name = "诸葛先生";
        person.age = 20;
        person.sex = '男';
    }

If you want to use this object later, just pass person there.
However, over time, we found that it is very unsafe to directly call attributes or assign attributes through '.' directly through person.name = "". For example, we only hope that this Mr. Zhuge can only be assigned a value, but not is valued, so what should I do? Next, look at the access rights of the object

object access rights

If we only want, we define a Mr. Zhuge object, but don't want to reveal his age, then how do we do it?
In java, three keywords for permission control are provided, which are almost identical to those in C++.
public, which can directly provide services to the outside world.
private, which cannot directly provide services to the outside world, but can only use
protected for its own internal protection. This keyword is used in inheritance. We will talk about this key when we talk about inheritance later. Character.
Let's redefine this class


public class Person {
    private int age;
    private String name;
    private char sex;   
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public void say(){
        System.out.println("我是" + this.name);
    }   
}

We still define a person object

Person person = new Person();
person.setAge(20);
person.setName("诸葛先生");
person.setSex('男');

In this way, we also construct a Mr. Zhuge object, but you will find that the three properties here are all modified by private, and age is not provided with a get method, at this time, you will not be able to hear person.age Or person.getAge() can be used to obtain Mr. Zhuge's real age. This is also a permission control strategy. Careful students may find that there is an additional this on it. What is this? In fact, this refers to the current object. If you define a person1 object, then this is the representative person1 object. If you define a person2 object, then this is the person2 object of the code, that is, this this refers to bytes.

Constructor

Some people will say that the above initialization method is too troublesome. We construct a Mr. Zhuge object, and we have to call person 3 times later... What, is there a simpler way? The answer is yes.

public class Person {
    private int age;
    private String name;
    private char sex;   
    public Person(int age,String name,char sex){
        this.age = age;
        this.name = name;
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public char getSex() {
        return sex;
    }
    public void say(){
        System.out.println("我是诸葛先生");
    }
}

At this time, when we use

Person person = new Person(20,"诸葛先生",'男');
System.out.println(person.getName());

The naming rule of the constructor is that the name must be the same as the class name, and there is no return value. If public is not written in front of the constructor, then the default permission package permission, that is, the same level directory, can be used, otherwise new will fail, in C++ In , if we don't write anything in front of the constructor, the compiler defaults to private, and in java, we call it default

inherit

We learned about classes and objects earlier. If we want a class to reuse the properties and methods of other classes, we can use inheritance to achieve it.

public Animal{
    protected String name;
    protected char sex;
    public void setName(String name){
        this.name = name;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public void getName(){
        return this.name;
    }
    public void getSex(){
        return this.sex;
    }
    public void say(){}
}

public Dog extends Animal{
    public void say(){
        System.out.println("汪");
    }
}
public Cat extends Animal{
    public void say(){
        System.out.println("喵");
    }
}

We define an Animal object, and let dog and cat inherit from animal. At this time, the methods in animal are also included in these two classes, but if animal is modified with private, it will not inherit this property or method.

Cat cat = new Cat();
car.setName("阿猫");
System.out.println(cat.getName);
Dog dog = new Dog();
dog.setName("阿狗");
System.out.println(dog.getName());

When using inheritance, the execution order of constructors

When I use inheritance, in fact, the constructor of the parent class is executed first, and then the constructor of the subclass is executed. In memory, in fact, there are two objects, both of which are referenced by the same identifier .

public class Test extends Test2{
    public Test(){
        super("哈哈");
        System.out.println("我是子类的构造方法");
    }
    public static void main(String[] args) {
        System.out.println(new Test().getName());;
    }
    public int getNum(){
        return 1;
    }
}

class Test2{
    protected String name;
    public Test2(String name){
        this.name = name;
        System.out.println("我是父类的构造方法");
    }
    public String getName(){
        return this.name;
    }
    public int getNum(){
        return 1;
    }
}

You will find that I am the construction method of the parent class is printed first, and then I am the construction method of the subclass, and then print haha, this is the system, the construction method of the parent class is executed first, if we do not Write this super ("haha"), then there is a problem with this side program. If you don't write it, the compiler will automatically call the parent class noon parameter construction method for us. We know that if we define other parameters in the parent class Constructor, then, there will be no default definition of a no-argument constructor, no matter what, the constructor of the parent class is called first.

Parent class reference points to child class object

With the above example,
we can do this completely and let the parent class reference point to the child class object.

Test2 t = new Test();
System.out.println(t.getNum());

The above prints 2, not 1, that is, if the subclass and the superclass have the same method, then the call is to execute the method in the subclass, not the method in the superclass, which is polymorphism, Think about it, if many classes inherit this Test, there is a getNum() method, do we want to call the method in which class, we can just new which class? We don't have to care which class to use to reference , because they can all be referenced by the parent class. Take the example of one of the animals above.
Both cat and dog inherit animal, and we now have the following function

public static void listen(Animal animal){
    System.out.println("我听到有动物在喊" + animal.say());
}

You will find that if you use this method, you don't need to care whether the dog or cat is passed, it can call say(); method, with this, can we implement and define a set of interfaces for later use? I don't know what the other party wants to write in say, but I can indeed call the other party's say method here. This is the magic of polymorphism. You will find that it can actually call the method defined by others in the future. . In fact, this place is also explained in the C++ Quick Start, but the virtual keyword must be used in C++, otherwise polymorphism will not occur, and its own method will be called.

abstract class

A class with an abstract method is called an abstract class. An abstract method, that is, a method template, has no implementation in it. In the above example, you will find that we have not written anything in Animal. At this time, it can be defined as an abstract method.
How to define an abstract class

public abstract class Animal{
    protected String name;
    protected char sex;
    public void setName(String name){
        this.name = name;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public void getName(){
        return this.name;
    }
    public void getSex(){
        return this.sex;
    }
    public abstract void say();
}

In an abstract class, there can be abstract methods as well as ordinary methods.

#

interface

If a class is full of abstract methods, we can define it as an interface, and the interface definition method is

public interface A{
    public void say();
}

implement this interface

public class B implements A{
    @Override
    public void say(){
        System.out.println("hello");
    }
}

So, what is the benefit of defining it as an interface? Why not use inheritance? The reason is that using an interface can also achieve polymorphism. In java, only single-inherited methods are allowed, so if you inherit here, there will be no positional inheritance in the future. It is defined as an interface here, which lays the foundation for the scalability of the program. foundation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463361&siteId=291194637
Recommended