Current location - Training Enrollment Network - Early education courses - Dog my Dog = new Dog(); What the hell does this mean? In JAVA.
Dog my Dog = new Dog(); What the hell does this mean? In JAVA.
Dog is a class, and the type of the variable myDog is specified as Dog.

MyDog is a reference variable name, and its type is Dog.

= is an assignment.

New can be understood as "generation".

Dog () calls the parameterless constructor of the Dog class. If it is not defined in the Dog class, the system defaults to a parameterless constructor.

Define a myDog variable with type: Dog and value: a Dog object.

ABC x = new ABC(); This is ok, provided that there is abc class.

ABC x = new BCD(); This will be divided into two situations.

1. If abc is the parent class of bcd, or bcd implements the interface abc, it will be fine.

2. Otherwise, you can't.