Classes and Objects--Java

A class is a custom type, like a template

Objects are like Entities

Multiple objects can be generated through a class

Object Oriented Concept OOP

Object-oriented is a way of thinking about problems, and it is a kind of thinking. For example: concepts and examples. Theory and Practice. name and reality A class is a general term for a class of objects. An object is an instance of this type of reification. The benefits of object-oriented: to simplify complex things, just face an object.

Object-oriented: find objects, create objects, use objects

Three important ideas of object-oriented OOP: encapsulation inheritance polymorphism

Example:

Process-oriented: 1. Open the refrigerator 2. Put the elephant in 3. Close the refrigerator Object-oriented: Opening the refrigerator, storing it, and closing it are all operations on the refrigerator, which is the behavior of the refrigerator. The refrigerator is an object, so as long as the functions of the refrigerator are operated, they must be defined in the refrigerator.

Java is an object-oriented language

Class definition and member access

Class names are big camelCase

class Person {  //A class is a custom type
    //Field - member variable (defined outside the method, inside the class)
    //Instance member variables are placed inside the object, and the object is on the heap
    //The instance member variable is not initialized, it is the default value of 0, the reference type is null, the basic type is 0, and the default value of char is '\u000' 
    //The default value of boolean is false
    public String name;
    public int age;
    //Static member variables -- also called class variables, also conform to the above default value rules  
    //Static member variables do not depend on objects and belong to the method area, so write the class name when printing. Static member variables
    //Static things will only be opened in the method area, and the following per will be opened repeatedly 
    public static int b=20;
    //Method - Behavior
     public int a=10;//Local variables--defined in the method, local variables must be initialized, and member variables may not be initialized  
    public void eat() {
        System.out.println("eat");
    }
    public void drink() {
        System.out.println("drink");
    }
    public static  void func(){  //static method  
        System.out.println("func");
    }

}
public class Class1 {
    public static void main(String[] args) {
        //Instantiate an object through the keyword new
        Person per = new Person();//At this point Person is a custom type, per is called a reference variable
        per.eat();
        per.drink();
        System.out.println(per.age);
        System.out.println(per.name);
        System.out.println(Person.b); //Static member variables do not depend on objects, directly class name. Static member variables
        Person.func;//Calling static methods is also a direct class name. Static member variables
    }
}
//Note: Person is the class name
//      per is an object, also called a reference variable 
//Instance member variables belong to objects, and static member variables do not depend on objects, so you can use class names directly without creating objects. Static member variable names/method names
copy

In the figure above, the class Person is like a template, both per and person2 are objects, and both can use the Person class

Notice:

1. Static member variables can only be defined outside the method, not inside the method

2. Common methods cannot be defined in static methods, and static methods can be defined in common methods

This is because static does not depend on objects, but ordinary ones depend on objects, so ordinary methods cannot be defined in static methods

access modifier qualifier

public shared private private protected protected default write nothing default permissions --package access permissions

Reference types include String Array...

static keyword

1. Modified attributes 2. Modified methods 3. Code blocks (to be written later) 4. Modified classes (we will learn about internal classes later)

1. Modify attributes

class TestDemo{   //The class name is TestDemo
    public int a;//instance member variable
    public static int count;//static member variable 
}
public class Class1 {
    public static void main(String[] args) {
            TestDemo t1 = new TestDemo();
            t1.a++;//1
            TestDemo.count++;//1
            System.out.println(t1.a);
            System.out.println(TestDemo.count);


            System.out.println("============");
            TestDemo t2 = new TestDemo();
            t2.a++;//1
            TestDemo.count++;//2
            System.out.println(t2.a);
            System.out.println(TestDemo.count);
    }
//t1 t2 are objects of class    
//As long as the instance member variable is new, it must be recreated once, and the data on the heap will disappear
//Static member variables are placed in the method area and will only be generated once, so they have memory
copy

2. Modification method

class TestDemo{   //The class name is TestDemo
    public int a;//instance member variable
    public static int count;//static member variable
    public static  void func(){  //static method
        System.out.println("func");
        a=12;//err
        count=23;//Non-static data members cannot be called inside a static method, only static member variables or static methods can be called
        //Because static members do not depend on objects, static methods can be called directly
    }
}
copy

So before writing the method, you need to add static, because you can call the method directly without instantiating an object

public class Class1 {
    public static void fun1() {
       //......
    }
    public static void fun2() {
     //......
    }
    public static void main(String[] args) {
        fun1();//call the static method directly
        //Same effect:
        Class1 pc = new Class1();//If you want to use the ordinary method, you must first define an object pc, and then call fun2, which is troublesome
        Class1.fun2();
 }
copy

Summary: All methods or properties modified by static do not depend on objects.

Encapsulation (to match the interface)

Encapsulation: modify properties (fields) or methods with private, and provide pubiic methods for callers of the class to use

The advantage of encapsulation: private data members cannot be called outside the class, which is more secure

With private, it is necessary to provide get and set interfaces to make them accessible

class Student{
    private String Myname;//Adding private is encapsulation --- restricting Myname to be used only in classes
    public int age;
    
    //Provide an interface to get name
    public String getMyname() {
        return Myname;  //The return value is String
    }

    //Modify the interface of Myname
    public void setMyname(String name) {
        Myname = name;
    }

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

public class TestDemo2 {
    public static void main(String[] args) {
        Student stu = new Student();//define an object
           stu.setMyname("deng");
        System.out.println(stu.getMyname());
    }
}
//Two interfaces are provided to set and use encapsulated variables outside the class
//With encapsulation, you don’t need to care about the variable naming in the class, just pay attention to the name of the calling interface
copy

Autocomplete interface

Put the cursor in the class, alt + insert will appear getter and setter, go down to find getter and setter, shift + down key to select, and also automatically add this, very convenient

Tags: C++ OOP

Posted by pdunn on Thu, 12 Jan 2023 17:28:50 +0530