Java object oriented

Posted by MrRosary on Thu, 20 Jan 2022 13:02:19 +0100

Java object oriented (3)

Relationship between class and object

class

Class is an abstract data type. It is the overall description / definition of a certain kind of things, but it can not represent a specific thing

  • For example: people, plants, mobile phones, computers
  • For example: Person class, Pet class, Car class, etc. these classes are used to describe / define the characteristics and behavior of a specific thing

object

Objects are concrete instances of abstract concepts

  • Zhang San is a concrete example of man
  • Wangcai of Zhang San's family is a concrete example of dogs
  • Concrete instance: it is a concrete instance rather than an abstract concept that can reflect the characteristics and functions

create object

  • Create an object using the new keyword
  • When using the new keyword to create: allocate memory space, initialize the created object by default, and call the constructor in the class

class

  • A class contains only properties and methods
//class
//Student class
public class Student {
    //attribute
    String name;
    int age;
    //method
    public void study(){
        System.out.println(this.name+"I'm learning");//This represents this class
    }
    public void play(){
        System.out.println(this.name+"Playing");
    }
}
//Main function
//The main function instantiates the class
public class Application {
    public static void main(String[] args) {
        //Instantiate a class
        //Class will return its own object after instantiation
        //The std1 and std2 objects are a concrete instance of Student
        Student std1 = new Student();
        Student std2 = new Student();
        System.out.println(std1.name);//Output defaults
        System.out.println(std2.name);//Output defaults
        //assignment
        std1.name="Xiao Ming";
        std2.name="Xiao Hong";
        System.out.println(std1.name);//Output the assigned value
        System.out.println(std2.name);//Output the assigned value
        std1.study();
        std2.play();
    }
}
//Output:
null
null
 Xiao Ming
 Xiao Hong
 Xiao Ming is studying
 Xiao Hong is playing

constructor

  • Constructors are also called construction methods

  • The constructor must be called when creating an object

  • Characteristics of constructor:

    1. Must be the same as the class name
    2. There must be no return type and void cannot be written
  • Even if a class doesn't write anything, it will have a default constructor (just not shown)

    //A class
    public class Student {
    }
    //Nothing is written, but it actually has a default constructor
    public class Student {
        //The default constructor (constructor) can be used without writing it
        public Student(){     
        }
    }
    //use
    public class Application {
        public static void main(String[] args) {
            Student3 student3 = new Student3();
            System.out.println(student3.name);
        }
    }
    //Output as default initial value
    //Output:
    null
    
  • Function of constructor:

    • Instantiate the initial value (the default parameterless construction method will use the default initial value)

      Nonparametric Construction:

      public class Student {
          String name;
          //Self written construction method
          //Parameterless construction, no parameters
          public Student(){
              this.name = "Shi Xiaopeng";//Initialize the name value to Shi Xiaopeng
          }
      }
      //use
      public class Application {
          public static void main(String[] args) {
              //new instantiates an object
              Student student = new Student();
              System.out.println(student.name);
          }
      }
      //Output:
      Shi Xiaopeng
      

      Parametric structure:

      public class Student2 {
          String name;
          //Write your own construction method
          //Parameterized construction. When instantiating an object, you can add parameters to it
          public Student2(String a){
              this.name=a;//this refers to the current class
          }
      }
      //use
      public class Application {
          public static void main(String[] args) {
              //new instantiates an object
              String a="Shi Xiaopeng";
              Student2 student2 = new Student2(a);
              System.out.println(student2.name);
          }
      }
      //Output:
      Shi Xiaopeng
      
  • Once a parametric construct is defined:

    • The default parameterless construction cannot be used
    • You can only write a parameterless construct yourself

    Example 1:

    public class Student2 {
        String name;
        //Parametric structure
        public Student2(String a){
            this.name=a;
        }
        //Since the parameterized structure is written above, the default nonparametric structure cannot be used. You can only write the nonparametric structure yourself
        //Write your own parameterless structure
        public Student2(){
            this.name="Xiao Ming";//this refers to the current class
        }
    }
    public class Application {
        public static void main(String[] args) {
            //new instantiates an object
            //Have reference
            String a="Shi Xiaopeng";
            Student2 std1= new Student2(a);
            System.out.println(std1.name);
            //new instantiates an object
            //No parameter
            Student2 std2 = new Student2();
            System.out.println(std2.name);
        }
    }
    //Output:
    Shi Xiaopeng
     Xiao Ming
    

    Example 2:

    public class Student2 {
        String name;
        public Student2(String a){
            this.name=a;
        }
        //Since the parameterized structure is written above, the default nonparametric structure cannot be used. You can only write the nonparametric structure yourself
        public Student2(){
        }
    }
    public class Application {
        public static void main(String[] args) {
            //new instantiates an object
            //Have reference
            String a="Shi Xiaopeng";
            Student2 std1= new Student2(a);
            System.out.println(std1.name);
            //new instantiates an object
            //No parameter
            Student2 std2 = new Student2();
            System.out.println(std2.name);
        }
    }
    //Output:
    Shi Xiaopeng
    null