day 7 inheritance, etc.

day7

Class inheritance
java only supports single inheritance, multiple inheritance is not allowed
a subclass can have only one parent
a parent can derive subclasses

Q1:Here Insert Picture Description

public class TestCylinder {
	double radius;
	
	public void Circle(double radius) {
		radius = 1; 
		this.radius = radius;
	}
	
	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double findArea() {
		
		return  3.14 * this.radius * this.radius;
	}

	public static void main(String[] args) {
		
		TestCylinder tc = new TestCylinder();
		
		tc.setRadius(1);
		System.out.println(tc.getRadius());
		System.out.println(tc.findArea());
		
	}
	
}
public class Cylinder extends TestCylinder{
	double length;
	
	public Cylinder() {
		length = 1;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
	
	public double findVolume() {
		return 3.14 * this.radius * this.radius * this.length;
	}
	
	public static void main(String[] args) {
		
		Cylinder c = new Cylinder();
		
		c.setLength(7);
		c.setLength(6);
		System.out.println(c.findVolume());
	}
}

Tried it can be calculated, but the teacher did not explain, do not know is correct.

Rewriting process
can be carried out as necessary in the subclass method inherited from the parent class to transformation, also called the reset process, covered
during program execution, the method subclass will override the parent class
1 heavy write method must have the same method name and method are rewritten, the parameter list and return type.
method // subclass overrides the parent class, but writing code rewriting (braces) body method, if the parent class the method is public, the subclass can not override the default following
2. overridden method can not be rewritten to use more stringent than the method of access.
3. rewriting and rewriting methods are required for both the static, or non-simultaneously. the static
4. subclass method throws exception Yichang not be greater than the parent process is overwritten.

In the eclipse at the subclass alt + / exhale override method
Here Insert Picture Description
Here Insert Picture Description

If the parent class and subclass in the same package, then the parent class for members of the private modifier if not private, it can be used.

Key words super
super can pay to use the parent class member variables can be traced using the method of the parent class members
can use super drill-up parent parent, of course, can always drill up, so if there are multiple levels of parent class, then .
in a subclass, call the parent class constructor, the constructor must be placed in the first row

Polymorphic
1. The need exists to inherit or implement relationship
2. to have to cover operating
member method:
1, compile-time, whether the method has been called class variable belongs to view references.
2. runtime, call the class of the actual object belongs rewriting method.
member variables:
do not have the polymorphism, but only refer to a class variable belongs.

instancof operator

Object class instanceof
detecting whether the object is an object type, and returns a Boolean value
// Person parent, Student subclass
Here Insert Picture Description

Object class
Object class is the parent class of all the java class, the base class has become

public class Person{
}
等价于
public class Person extends Object {
}

Released eight original articles · won praise 0 · Views 82

Guess you like

Origin blog.csdn.net/Reishinj/article/details/105011632
Recommended