Home > Back-end >  Java classes and inheritance for answers
Java classes and inheritance for answers

Time:09-18

Public class Test {
Public static void main (String [] args) {
Shape Shape=new Circle ();
System. Out. Println (shape, name);
Shape. Named printType ();
Shape. PrintName ();
}
}

The class Shape {
Public String name="shape";

Public Shape () {
System. The out. Println (" shape constructor ");
}

Public void named printType () {
System. The out. Println (" this is shape ");
}

Public static void printName () {
System. The out. Println (" shape ");
}
}

The class Circle extends Shape {
Public String name="circle";

Public Circle () {
System. The out. Println (" circle constructor ");
}

Public void named printType () {
System. The out. Println (" this is circle ");
}

Public static void printName () {
System. The out. Println (" circle ");
}
}
The results
Shape constructor
Circle the constructor
Shape
This is circle
Shape
The two shape is zha output

CodePudding user response:

The static method is not dependent on instance, you create the object is the shape of a reference type, so he will call the shape method
  • Related