Java Basics - Object Oriented

Java Basics - Object Oriented


1. Object-oriented

  • Object-oriented concept: Object-oriented is relative to process-oriented, both object-oriented and process-oriented are a kind of idea. (Object-oriented is an idea based on objects that perform functions)
  • Object-oriented characteristics: It is a kind of thinking that conforms to people's thinking habits, which can simplify complex things and convert programmers from executors to conductors.
  • Object-oriented features: encapsulation, inheritance, polymorphism.

1.1 Packaging

  • Concept: It refers to hiding the properties and implementation details of an object, and only provides public access to the outside world.
  • benefit:
    1. Isolate changes.
    2. Easy to use.
    3. Improve reusability.
    4. Improve security.
  • Packaging principles:
    1. Hide everything you don't want to make available to the public.
    2. Hide properties and provide public methods to access them. Such as: getXXX, setXXX.

1.2 Inheritance

  • Description of the description:
    • Use the extends keyword to create an inheritance relationship between classes.
    • When multiple classes have the same attributes and behaviors, these contents are extracted into a single class, then multiple classes do not need to define these attributes and behaviors, just inherit that class. Multiple classes can be called subclasses, and a single class is called a parent or superclass.
    • Subclasses cannot inherit private classes from parent classes.
  • Advantages of inheritance:
    • The emergence of inheritance improves the reusability of code.
    • The emergence of inheritance has created a relationship between classes and provided the premise of polymorphism.
  • Inherited features:
    • Java only supports single inheritance, not multiple inheritance.
    • A class has only one parent class and cannot have multiple parent classes.
    • Java supports multiple layers of inheritance (inheritance system)
  • Function coverage:
    • When the member function of the child parent class is exactly the same, the function of the child class will be run. This phenomenon is called overriding operation, which is the characteristic of functions in child parent classes.
    • In the subclass overriding method, the continued use of the overridden method can be obtained by super.function name.
  • Example:
    • Code:
class Person 
{
    String name;
    int age;
    void study()
    {
        System.out.println("Person……"+age);
    }
}
//Student继承了Person类
class Student extends Person
{
    //继承了父类中的成员变量
    //复写了Person类中study方法
    void study()
    {
        System.out.println("student study……"+age);
    }
}
class ExtendDemo
{
    public static void main(String args[])
    {
        Student s=new Student();
        s.name="张三";
        s.age=20;
        s.study();
    }
}
  • Output result:
    write picture description here

1.3 Polymorphism

  • Definition: The various forms of existence of a certain type of thing.
  • Embodiment: The reference of the parent class or interface points to or receives its own subclass object.
  • Role: The existence of polymorphism improves the scalability and later maintainability of the program.
  • premise:
    1. There needs to be an inheritance or implementation relationship.
    2. An override operation is required.
  • Benefit: Improve the scalability of the code, and the code defined in the early stage can use the content in the later stage.
  • When polymorphic, the characteristics of members:
    1. Member variables
      • Compile time: refer to whether there is a called member variable in the class to which the reference variable belongs. Yes, the compilation passes, no, the compilation fails.
      • Runtime: Refer to whether there is a called member variable in the class to which the reference variable belongs, and run the member variable in the class to which it belongs.
      • Simply put: both compile and run refer to the left side of the equal sign.
    2. member function (non-static)
      • Compile time: refer to whether there is a function called in the class to which the reference variable belongs. Yes, the compilation passes, no, the compilation fails.
      • Runtime: Whether there is a function called in the class to which the referenced object belongs.
      • Simply put, compile on the left and run on the right.
    3. static function
      • Compile time: The reference is whether there is a function called in the class to which the object belongs.
      • Runtime: Refers to whether there is a function called in the class to which the object belongs.
      • Simply put: Compile and run look on the left.
  • Example:
    • Code:
class Fu
{
    int age=40;
    public static void see()
    {
        System.out.println("张父");
    }
}
//继承Fu类
class Zi extends Fu
{
    int age=4;
    public static void see()
    {
        System.out.println("张三");
    }
}
class  Demo
{
    public static void main(String[] args) 
    {
        //多态,父类引用接收子类对象 
        Fu x=new Zi();
        //静态函数编译和运行时都看父类引用
        x.see();
        //成员变量编译和运行时都看父类引用
        System.out.println(x.age);
    }
}
  • output result
    write picture description here

2. Classes and Objects

  • Definition of a class: A class is an abstract description of a concrete thing, a conceptual definition.
  • Definition of an object: An object is an entity that actually exists.
  • The relationship between classes and objects: A class is an abstraction of an object, and an object is an instance of a class.

2.1 Introduction to inner classes

  • Definition: A class is defined inside another class, and that class is called an inner class (built-in class, nested class).
  • Features of Access:
    • The inner class can directly access members in the outer class, including private members.
    • To access the members of the inner class, the outer class must create an object of the inner class.
  • Position of inner classes: inner classes are defined at member positions and can be modified by private and static. The inner class modified by static can only access the static members in the outer class.

2.2 Anonymous inner classes

  • Definition: It is a simplified way of writing inner classes.
  • Premise: An inner class can inherit or implement an outer class or interface.
  • Format:new 外部类名或者接口名(){覆盖类或者接口中的代码,(也可以自定义内容。)}
  • Simple understanding: It is to create an anonymous object of a subclass of an external class or interface with content.

2.3 Interface

  • Definition of interface: When the methods in an abstract class are abstract, then the abstract class can be defined and represented in another form, which is the interface.

  • Format:interface{}

  • Member modifiers in interfaces are fixed:

    • Member constants: public static final
    • Member function: public abstract
    • The members of the interface are public permissions.
  • Features:

    • Interfaces are rules that are exposed to the outside world.
    • An interface is a functional extension of a program.
    • A class can implement multiple interfaces.
  • Comprehensive case

    • Code:
//实现功能的扩展。
interface USB
{   
    //暴露的原则
    public abstract void open();
    public abstract void close();   
}
//实现的原则
//降低了耦合性
class Upan implements USB
{
    public void open()
    {
        System.out.println("Upan open");
    }
    public void close()
    {
            System.out.println("Upan close");
    }
}
class UsbMouse implements USB
{
    public void open()
    {
        System.out.println("UsbMouse open");
    }
    public void close()
    {
        System.out.println("UsbMouse close");
    }
}
class  PC 
{
    public static void main(String[] args) 
    {
        //功能扩展了
        UseUSB(new UsbMouse());
    }
    //使用原则
    public static void UseUSB(USB u)//接口类的引用,用于接收(指向)接口的子类对象
     {
        if(u!=null);
        {
            u.open();
            u.close();
        }
    }
}
  • Output result:

2.4 Objects

  • Object generation: The fundamental way to create an object is the constructor (special method), and an instance of this class can be created by calling the constructor of a class through the new keyword.

  • Anonymous objects:

    • Short form of object (an object without a name, an object is created, not assigned to a variable)
    • Usage:
      1. When there is only one call to an object method.
      2. When passed as an actual parameter.

2.5 The difference between member variables and static variables

1. The lifetime of the two variables is different:

  • Member variables exist as objects are created and are released as objects are reclaimed.
  • Static variables exist as classes are loaded and disappear as classes disappear.

2. The calling method is different:

  • Member variables can only be called by objects.
  • Static variables can be called by both objects and classes.

3. Aliases keep going:

  • Member variables are also known as instance variables.
  • Static variables are also known as class variables.

4. The data storage location is different:

  • Member variables are stored in objects in heap memory, so they are also called object-specific data.
  • Static variable data is stored in the static area of ​​the method area (shared data area), so it is called shared data of data.

3. Constructors and Constructor Code Blocks

3.1 Constructor

  • Features:

    1. The function name is the same as the class name.
    2. Do not define the return value type.
    3. There is no specific return value.
  • Action: Initialize the object.

3.2 Constructing code blocks

  • Role: All objects can be initialized.

Fourth, the singleton design pattern

  • Concept: The singleton design pattern is to ensure the object uniqueness of a class in memory.
  • Implementation steps:
    1. Private the class constructor.
    2. Create an object of this class in this class through new.
    3. Define a public method that returns the created object.

4.1 Hungry Chinese style

  • Code:
    class Single
    {
        //对构造函数进行私有化,无法在其它类中创建对象
        private Single(){}
        //在本类中创建一个本类对象
        private static Single s=new Single();
        //提供一个方法,将创建的对象返回
        public static Single getInstance()
        {
            return s;   
        }
    }

    class SingleDemo 
    {
        public static void main(String[] args) 
        {
            Single s1=Single.getInstance();
            Single s2=Single.getInstance();
            //s1,s2为同一个对象
            System.out.println(s1==s2);
        }
    }
  • Output result:

4.2 The Lazy Man

  • Code:
    class Single1
    {
        private Single1(){}
        //延迟加载形式,只有调用了getInstance方法时,才会创建对象。
        private static Single1 s =null;
        public static Single1 getInstance()
        {
            if(s==null)
                s=new Single1();
            return s;
        }
    }
    class SingleDemo1
    {
        public static void main(String args[])
        {
        Single1 s1=Single1.getInstance();
        Single1 s2=Single1.getInstance();
        //s1,s2为同一个对象
        System.out.println(s1==s2);
        }
    }
  • Output result:

Guess you like

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