第七周实验报告与总结5

一.抽象类的使用

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

2.编程技巧

 

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

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

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

(二)使用接口技术

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

编程技巧

 

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

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

代码1:

package chouxianglei;

abstract class shape {
    public abstract double print();  
}
class sanjiao extends shape {
    private double a;
    private double b;
    private double c; public sanjiao(double a,double b,double c){
        this.a=a;
        this.b=b;
        this.c=c;             
    }
    public double print() {
        double p=(a+b+c)/2;
        return  Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
}
class juxing extends shape {
    private double width;
    private double height;
    public juxing(double width, double height){
        this.height=height;
        this.width=width;   
    }
    public double print() {     
        return width*height; 
    }
}
class yuan extends shape {
    double radious;    
    public yuan(double radious){ 
        this.radious=radious; 
    }
    public double print() {    
        return Math.PI*radious*radious;
    }
}
public class chouxianglei {
    public static void main(String[] args){
        shape s1=new sanjiao(3,4,5);
        shape s2=new juxing(5,8);
        shape s3=new yuan(6);    
        System.out.println("三角形的面积为: "+s1.print());
        System.out.println("矩形的面积为: "+s2.print());
        System.out.println("圆的面积为: "+s3.print());    
    }
}

实验截图:

 

 

二)使用接口技术

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

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

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

代码2:

interface shape {
    public void size();
}

class Cricle implements shape {

    public double redius;

    public Cricle(double redius) {
        this.redius = redius;
    }

    public double getRedius() {
        return redius;
    }

    public void setRedius(double redius) {
        this.redius = redius;
    }

    public double SSS() {
        double s = Math.pow(redius, 2) * 3.14;
        return s;
    }

    public void size() {
        System.out.println("圆的大小为" + SSS());
    }

}


class zhixian implements shape {
    public double h;

    public zhixian(double h) {
        this.h = h;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double S() {
        double s = h;
        return s;
    }

    public void size() {
        System.out.println("直线的大小为" + S());
    }

}

public class test {

    public static void main(String[] args) {
        shape str1 = new Cricle(10);
        shape str2 = new zhixian(10);
        str1.size();
        str2.size();
    }

}

截图2:

 

 

学习总结:

1.抽象类和接口都不能直接实例化,

2.抽象类必须通过对象的多态性进行实例化操作,且抽象类要被子类继承,接口要被类实现。

3.抽象类中的变量是普通变量,接口里定义的变量只能是公共的静态的常量。

4.抽象方法要被实现,不能是静态的,不能是私有的。

5.接口可继承接口,而且可以多继承接口,但类只能单根继承。

 

posted @ 2019-10-12 20:28  gyyyy  阅读(388)  评论(0编辑  收藏  举报