Java object-oriented programming

1. What is object-oriented programming

   OOP (Object Oriented Programming, so often called OOP) is not unique to Java, but a programming idea, Java, C ++, Python has its realization. Its essence is reflected in the model of abstract thought process and object-oriented approach to establish. Model is used to reflect the characteristics of things in the real world, any model can not reflect all the specific characteristics of objective things, only an abstract things and variety rule, and more generally in the range it covers, more focused and deeper description of the features of the object. By establishing the model to achieve the abstract is to deepen people's understanding of the object.

2, OOP advantages and disadvantages

Pros: Easy to maintain and expand, high code reusability. Since there are object oriented encapsulation, inheritance, polymorphism characteristic, the system can be designed low coupling, making the system more flexible.
Drawback: CPU overhead of large, occupying more resources

3, the three characteristics

Package: hidden object properties and implementation details, retaining only some of the external interfaces;
Inheritance: subclasses inherit the attributes and methods of the parent class;
polymorphism: a plurality of capability or form of different forms of the same behavior.

3.1, the package

   Access modifier used to achieve control of the properties and methods, including private: This class can only access; default (default permissions): This package can only access; protected: and subclasses packets in succession can be accessed only ; public: accessible. We then define an object class, usually hidden attributes, providing a common set, get access methods.

/**
 * @author RuiMing Lin
 * @date 2020-03-05 16:01
 */
public class Person {
    
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    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;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.2, inheritance

   Inheritance allows access to all non-private subclasses attributes and methods of the parent class, code reuse is reduced. Inheritance can make the object hierarchy, type clearer.

/**
 * @author RuiMing Lin
 * @date 2020-03-05 18:18
 */
public class Demo {
    public static void main(String[] args) {
        Son s = new Son("小明",23);
        s.eat();        //从父类继承的方法
        s.sleep();      //重新父类的方法
        s.study();      //子类自己定义的方法
    }
}

class Father{
    String name;
    int age;


    public Father(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat(){
        System.out.println("吃饭");
    }

    public void sleep(){
        System.out.println("父亲睡觉");
    }
}

class Son extends Father{
    public Son(String name, int age) {
        super(name, age);
    }

    @Override
    public void sleep(){
        System.out.println("儿子睡觉");
    }

    public void study(){
        System.out.println("儿子学习");
    }
}

3.3, polymorphic

public abstract class Animal { 
	public abstract void eat(); 
}
class Cat extends Animal { 
	public void eat() { 
		System.out.println("吃鱼"); 
	} 
}
class Dog extends Animal { 
	public void eat() { 
		System.out.println("吃骨头"); 
	} 
}
public class Test { 
	public static void main(String[] args) { 
	// 多态形式,创建对象 
	Cat c = new Cat(); 
	Dog d = new Dog(); 
	// 调用showCatEat 
	showCatEat(c);
	// 调用showDogEat
	showDogEat(d); 
	showAnimalEat(c); 
	showAnimalEat(d); 
}
	public static void showCatEat (Cat c){ 
		c.eat(); 
	}
	public static void showDogEat (Dog d){ 
		d.eat(); 
	}
	public static void showAnimalEat (Animal a){ 
		a.eat(); 
	} 
}

   Since the characteristics of the support polymorphism, Animal showAnimalEat type of method, Dog and Cat is the parent class type, type of parent receiving subclass object, the object can pass Cat and Dog object to the method. When the eat method executed, predetermined polymorphism, a subclass overrides a method performed, then consistent with the effect of natural showCatEat, showDogEat method, you can replace the showAnimalEat above two methods. Not just an alternative, in terms of scalability, whether after more sub-categories appear, we do not need to write showXxxEat method, the direct use of showAnimalEat can be done. Therefore, the benefits of multi-state, embodied in the program can be written in simpler, and good extension.

3.3.1, multi-state operating rules

  If the compile parent (parent interface) method or the own class, the error does not use IDE; see the operation of a particular type, class or if the class of the method, the error will not use IDE.

  1. Member variables: compile Look left, look at the left side of the run;
  2. Static methods: compile Look left, look at the left side of the run;
  3. Non-static method: compile Look left, look right of the run.

4, summary

  Object-oriented programming ideas different from the process-oriented programming, its use and more stringent specifications. Java object-oriented programming is there are many more standardized, more in-depth study of the need for Java will understand it better.

Please indicate the wrong place! Thought that it was in trouble if you can give a praise! We welcome comments section or private letter exchange!

Published 30 original articles · won praise 72 · views 10000 +

Guess you like

Origin blog.csdn.net/Orange_minger/article/details/104680093