Java object-oriented basic syntax

1.
The simple syntax for defining a class in Java is as follows:

[修饰符] class 类名
{
    零个到多个构造器定义
    零个到多个成员变量
    零个到多个方法
}

Member variables are used to define the state data contained in the class or an instance of the class, and methods are used to define the behavioral characteristics or functional realization of the class or an instance of the class. The constructor is used to construct an instance of the class. The JAVA language uses the new keyword to call the constructor to return an instance of the class.

Inheritance of classes in Java is achieved through extends. The syntax for a subclass to inherit a parent class is as follows:

[修饰符]class SubClass extends SuperClass
{
    //类定义部分
}

If the subclass inherits the superclass, the phenomenon that the subclass contains the method with the same name as the superclass is called method overriding.
For example, the following defines a subclass of the weapon class AK47

public class weapeon
{
    public double weight;
    public int reload;
    public void shot()
    {
        System.out.println("突突突");
    }
}
public class AK47 extends weapeon
{
    public void shot()   //方法的重写
    {
        System.out.println("biubiu");
    }
    public static void main(String[] args)
    {
        AK47 one = new AK47();
        AK47.shot();   //将输出biubiu
    }
}

Java allows multiple methods with the same name to be defined in the same class, as long as the parameter lists are different. If a class contains two or more methods with the same name but different parameter lists, it is called method overloading.
E.g:

public class testClass
{
    public void test()
    {
        System.out.println("无参数");
    }
    public void test(String msg)
    {
        System.out.println(msg);
    }
    public static void main(String[] args)
    {
        testClass one = new testClass();
        one.test();
        one.test("Hello");
    }
}

If you need to call the overridden instance method of the parent class in the subclass method, you can use super qualification to call the overridden instance method of the parent class, such as super.say(); . super cannot appear in static modified methods.

2.
Method The simple syntax for defining a method is:

[修饰符] 方法返回值类型 方法名(形参列表)
{
    //由零条到多条可执行性语句组成的方法体
}

The modifier can be omitted, or it can be public, protected, private, static, final, abstract, of which public, protected, private can only appear at most one of them, abstract and final can only appear at most one of them, they can be combined with static Up modification method.

The return value type can be any data type allowed by the JAVA language, including basic types and reference types; if a method return value type is declared, there must be a valid return statement in the method body, which returns a variable or expression, this The variable or expression must match the declared type.

The naming rules for method names are basically the same as those for member variables.

The formal parameter list is used to define the parameters that the method can accept. The formal parameter list consists of zero to multiple sets of "parameter type parameter names", and multiple sets of parameters are separated by commas. Once the method is defined with a parameter list specified, the corresponding parameter values ​​must be passed in when calling the method.

3. Constructor
The simple syntax for defining a constructor is as follows:

[修饰符] 构造器名(形参列表)
{
    //零条或多条语句
}

Constructors are a special kind of method. The basic way to create an object is the constructor, and an instance of a class can be created by calling the constructor of a class through the new keyword.
Modifier: It can be omitted, or it can be one of public, protected, and private.
Constructor name: The constructor name must be the same as the class name.
Parameter list: Same as method.

Subclasses of Java cannot get constructors of superclasses.

Example: Create a Person class

class Person
{
    private String name;
    private int age;
    //定义一个构造器
    public setPersonInf(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    //定义一个方法
    public void say(String content)
    {
        System.out.println(content);
    }
}

Among them, the this keyword always points to the object that calls the method. The biggest function of this is to allow a method in a class to access another method or instance variable in the class. A statically decorated method cannot use this reference.

After an object is created, the methods and member variables defined in the class can be called through the class or instance if the access permissions allow it. The syntax for accessing a method or member variable of a class or instance is: class.class variable or method, or instance.instance variable or method.

4.
The basic syntax of interface definition:

[修饰符] interface 接口名 extends 父接口1,父接口2...
{
    //零到多个常量定义...
    //零到多个抽象方法定义...
    //零到多个内部类、接口、枚举定义...
    //零到多个默认方法或者类方法定义...
}

The interface defines the common behavior specification of multiple classes.

The modifier can be public or omitted. If omitted, the package permission access controller is used by default, that is, the interface can be accessed only under the same package structure.
It adopts the same rules as class names in naming, but according to convention, the first letter is capitalized, and it is formed by connecting multiple meaningful words.
An interface can have multiple direct parent interfaces, but can only inherit interfaces but not classes.

The main functions of the interface are:
(1) Define variables and can also be used for type conversion.
(2) Call the constants defined in the interface.
(3) Implemented by other classes.

The syntax for a class to implement an interface is as follows:

[修饰符]class 类名 extends 父类 implements 接口1、接口2...
{
    //类体部分
}

5. Initialization block
Similar to the constructor, the initialization block can initialize Java objects.
The basic syntax of an initialization block is as follows:

[修饰符]
{
    //可以包含任何可执行性语句
}

The modifier of an initialization block can only be static, and a static-modified initialization block is called a static initialization block. A static initializer block cannot access non-static members.

When creating a Java object, it is necessary to execute the initialization block and constructor of the top-level parent class first, then execute the initialization block and constructor of its parent class, and finally execute the initialization block and constructor of the class. In a way, initialization blocks are complementary to constructors.

6. The final keyword
The final keyword can be used to modify classes, variables and methods to indicate that the classes, methods and variables it modifies cannot be changed. Once a final-modified variable gets its initial value, the value of the final variable cannot be reassigned.
The places where final modified class variables and instance variables can specify initial values ​​are as follows:
(1) Class variables: the initial value must be specified in the static initialization block or when the class variable is declared, and only in one of the two places one of the specified.
(2) Instance variables: The initial value must be specified in a non-static initialization block, in the declaration of the instance variable or in the constructor, and can only be specified in one of three places.

When final modifies a reference type variable, final can only guarantee that the address referenced by the reference type variable will not change, that is, it always refers to the same object, but the object can be changed completely.

When a method is finalized, it means that the method cannot be overridden. If for some reason you don't want a subclass to override a method of the parent class, you can use final to decorate the method. Final modified methods can be overloaded.

When a class is finalized, the class cannot have subclasses.

7. abstract keyword
Abstract methods and abstract classes must be defined using the abstract modifier. Classes with abstract methods can only be defined as abstract classes, and abstract classes can have no abstract methods.
The rules of abstract methods and abstract classes (methods and classes modified by abstract) in Java are:
(1) An abstract method cannot have a method body. The modified method must be overridden by its subclass to make sense, so the abstract modified method cannot be defined as private access.
(2) The abstract class cannot be instantiated, and the new keyword cannot be used to call the constructor of the abstract class to create an instance of the abstract class. Abstract classes are mainly used to be called by their subclasses.
(3) A class with abstract methods can only be defined as an abstract class.

Reference books: "JAVA Madness Lecture Notes (3rd Edition)"

Guess you like

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