Experiments four types of inheritance
- Purpose
- We understood that use of abstract classes and interfaces;
- Understand the role of the package, grasp the design of the package.
- Experimental requirements
- Master the method of abstract class.
- Mastery of technology and the use of system interfaces to create a custom interface methods.
- Understand the structure of the Java system package.
- Able to create a custom package method.
- Content Experiments
- (A) using the abstract class
-
- Design of a class hierarchy, defines an abstract class - shape in which the abstract method including the area of the required shape. Inherit the abstract class defines triangle, rectangle, circle. Create a separate triangular, rectangular, round objects exist, the area of the output of various types of pattern.
NOTE: triangle area s = sqrt (p * (pa ) * (pb) * (pc)) wherein, a, b, c of three sides, p = (a + b + c) / 2
2. programming skills
(1) abstract class defines a method to achieve specific class;
(2) using the abstract class reference variable subclass can refer to objects;
(3) subclass object reference by the parent class, by which the actual reference using the method is a method to access the object subclass. All objects may be deposited into the parent class definition array.
- Design of a class hierarchy, defines an abstract class - shape in which the abstract method including the area of the required shape. Inherit the abstract class defines triangle, rectangle, circle. Create a separate triangular, rectangular, round objects exist, the area of the output of various types of pattern.
-
package java实验报告五; public class Test1 { public static void main(String[] args) { Circle cir=new Circle(1); Triangle tr=new Triangle(1,1,1); Rectangle re=new Rectangle(1,1); cir.getArea(); tr.getArea(); re.getArea(); } }
-
package java实验报告五; public class Circle extends Shape { private double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public Circle(double radius){ } public double getArea(){ double Area=Math.PI*getRadius()*getRadius(); System.out.println("圆的面积:"+Area); return Area; } }
package java实验报告五; public class Rectangle extends Shape { private double height; private double width; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public Rectangle(double height,double width){ this.height=height; this.width=width; } public double getArea() { double Area=getHeight()*getWidth(); System.out.println ( "rectangular area:" Area +); return Area; } }
-
package java实验报告五; public class Triangle extends Shape { private double a,b,c,p; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } public double getP() { return p; } public void setP(double p) { this.p = p; } public Triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; } public double getArea() { p=(getA()+getB()+getC())/2; double Area=Math.sqrt((p*(p-getA())*(p-getB())*(p-getC()))); System.out.println("三角形的面积:"+Area); return Area; } }
package java实验报告五; public class Test1 { public static void main(String[] args) { Circle cir=new Circle(1); Triangle tr=new Triangle(1,1,1); Rectangle re=new Rectangle(1,1); cir.getArea(); tr.getArea(); re.getArea(); }
-
(B) interface technology used
1 Interface Shape definitions, wherein the method comprises a size (), Design "straight line", "circle", Shape class implements the interface. Respectively, to create a "straight line", "circle" object, the size of various types of graphical output.
- Programming skills
Method (1) defined in the interface to be rewritten to achieve specific implementation of the interface class;
(2) using the interface type variables can be referenced object class is created that implements the interface.
-
five experimental report package java demo2; public interface the Shape { public void size (); }
-
five experimental report package java demo2; class the implements the Shape Line { public void size () { System.out.println ( "generates a straight line"); } }
-
five experimental report package java demo2; public class Circle the implements the Shape { public void size () { System.out.println ( "generates a circle"); } }
-
package java实验报告五demo2; public class Test1 { public static void main(String[] args) { Line l=new Line( ); l.size(); Circle c=new Circle(); c.size(); }
-
Summary: This week study abstract classes, interfaces, abstract classes, interfaces instantiated template design abstract class, standard interfaces. Learn a bit difficult, I irritated