蒹葭微青

导航

第七周课程总结&实验报告(五)

实验四 类的继承

  • 实验目的
  • 理解抽象类与接口的使用;
  • 了解包的作用,掌握包的设计方法。
  • 实验要求
  • 掌握使用抽象类的方法。
  • 掌握使用系统接口的技术和创建自定义接口的方法。
  • 了解 Java 系统包的结构。
  • 掌握创建自定义包的方法。

 

  • 实验内容

(一)抽象类的使用

  1. 设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
    注:三角形面积s=sqrt(p*(p-a)*(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2

2.编程技巧

(1)    抽象类定义的方法在具体类要实现;

(2)    使用抽象类的引用变量可引用子类的对象;

(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

实验代码:

abstract class Mianji {
    
    public abstract double area() ;
    
    
}
class Tangle extends Mianji {

    double a;
     double b;
     double c;
     double p;
     public Tangle(double a,double b,double c) {
         this.a=a;this.b=b;this.c=c;
     }
     
     
    public double area() {
        double p=(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
    

}
 class Yuan extends Mianji  {
     double r;
 
     public Yuan(double r) {
         this.r=r;
        
     }
    public double area() {
        
          return  Math.PI*r*r;
    }
    

 }
public class Rectangle{
     double width,height;
     public Rectangle (double w,double h){
         width=w;
         height=h;
     }
     public double arae() {
         return width*height;
     }
     
 }
public class TEST {

    public static void main(String[] args) {
        Mianji m1=new Tangle(2,3,4);
        Mianji m2=new Yuan(5);
        Rectangle m3=new Rectangle(6,7);
     System.out.println(m1.area());
     System.out.println(m2.area());
     System.out.println(m3.arae());
    }

}

实验过程:定义抽像方法,子类继承抽象类

实验结果截图

(二)使用接口技术

1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。

  1. 编程技巧

(1) 接口中定义的方法在实现接口的具体类中要重写实现;

(2) 利用接口类型的变量可引用实现该接口的类创建的对象。

实验过程

interface Shape {
    double size();

}
abstract class Line implements Shape{
    private double a,b,c,p;
    public Line( double a,double b,double c,double p) {
         this.a=a;this.b=b;this.c=c;this.p=p;    
        
    }
    public double size() {
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
        
        
    }
    
    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 class implementes  {
private double r;
public implementes(double r) {
    this.r=r;
}
public double size() {
    return 3.14*r*r;
}
}
public class Count{

    public static void main(String[] args) {
        Mianji m1=new Tangle(5,3,4);
        Mianji m2=new Yuan(5);
        Rectangle m3=new Rectangle(6,9);
     System.out.println(m1.area());
     System.out.println(m2.area());
     System.out.println(m3.arae());
    }

}

运行结果

 四、实验过程

本周老师在课堂上主要讲了:

1.为抽象类与接口实例,

2.抽象类的实际应用——制定标准,

3.设计模式——代理设计,

4.Object类(toString()方法和equals方法)

对interface还应该多练习。

五、结论

抽象类和接口在系统设计上都是用的最多的

 通过这次作业:我感到还有很多知识没学好,这次试验让我对super和this理解的更加透彻了:super与this不能同用,两者都不能不能出现在main方法中,不能在静态类中使用。好像进步的速度像乌龟那么慢,继续努力!

 

posted on 2019-10-10 18:29  蒹葭微青  阅读(209)  评论(0编辑  收藏  举报