Class and Object Details in Java

  • 2021-12-11 17:42:39
  • OfStack

Directory 1, Class Definition 2, Variable Types in Class 3, Constructor 4, Overload Method 5, Inheritance 5.1 Override Method 6, Create Object 7, Access Instance Variables and Method 8, Compare Object 8.1 Use == Compare Object 8.2 Use equals () Compare Object

Class can be thought of as a template for creating Java objects

1. Definition of class


public class Dog {
    String name;
    int age;

    void eat() {
    }
    void sleep() {
    }
}

2. The type of variable in the class

Local variables: Variables defined in a method or statement block are called local variables. Variable declaration and initialization are both in the method, and after the method ends, the variable will be destroyed automatically.

Member variable (instance variable): A member variable is a variable defined in a class outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods in classes, constructors and statement blocks of specific classes.

Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static Type.

3. Construction method

The name of the constructor must have the same name as the class, and a class can have multiple constructors.

When creating an object, at least one constructor must be called. If you do not explicitly define a constructor for the class, Java The compiler will provide a default constructor for this class.


public class Dog {
 public Dog() {
  System.out.println(" Structure Dog");
 }
}

4. Overloaded method

When you create two methods with the same name and different parameter lists (that is, different number or type of parameters), you have one overloaded method.

At run time, JRE Decide which variant of your overloaded method to call based on the parameters passed to it.

We can even call another overloaded method with the same name in one method.

Note, however, that when using overloaded methods,

You cannot overload a method just by changing its return type. You cannot have two methods with the same name and the same parameter list.

5. Inheritance

To indicate that a class inherits from a superclass, we can declare the class in the className Add after extends ,

For example:


public class Employee extends Person {
  // Employee  Inherit from  Person
  public Employee() {
    super();
    }
  
    public Employee(String name, int age, int height, int weight,
  String eyeColor, String gender) {
        super(name, age, height, weight, eyeColor, gender);
  }
}

In the constructor, call the super([args]) To initialize the parent class.

5.1 Override Methods

If a 1 subclass provides its own implementation of a method defined in its parent class, this is called a method override.

The override is similar in form to an overload, but requires the addition of a @Override . For example, when there is 1 in the parent class foo() Method, we can override it in a subclass as follows:


@Override
public int foo() {
 // TODO Auto-generated method stub
 return super.foo();
}

[Note] This code is automatically generated using Eclipse, which simply calls the method in the parent class (super. foo ()), without any change, but we wouldn't use it in practice.

When overridden, use the method() Call the rewritten function, but we can still pass the super.method() Invokes the method (before overriding) in the parent class.

6. Create an object

Objects are created from classes. Creating an object requires the following three steps:

Declare: Declare 1 object, including object name and object type. Instantiation: Using keywords Java0 To create 1 object. Initialization: Use the Java0 When an object is created, the constructor is called to initialize the object.

public class Dog {
 public static void main(String[] args) {
  Dog Dog0 = new Dog();
 }
}

7. Access instance variables and methods

Variables accessing instances: Instance name. Variable name Method that calls the instance: Instance name. Method name ()

public class Dog {
 String name;
 int age;

 void eat(String food) {
  System.out.println(name + " is eating " + food + ".");
 }

 public Dog() {
  name = "Dog";
  age = 0;

  System.out.println(" Structure () : ");
  System.out.println(name + "\t" + age);
 }
 public Dog(String dogName, int dogAge) {
  name = dogName;
  age = dogAge;
  System.out.println(" Structure (name, age) : ");
  System.out.println(name + "\t" + age);
 }

 public static void main(String[] args) {
  Dog Dog0 = new Dog();
  Dog Dog1 = new Dog("FooBar", 3);

  //  Access variable  
  Dog0.name = "Ana";
  System.out.println(Dog0.name);

  //  Access method 
  Dog1.eat("cat");

 }
}

Running


Related articles: