zoukankan      html  css  js  c++  java
  • 第七周课程总结&实验报告(五)

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

    实验代码:

    package hello;
    
    import java.util.Scanner;
    
    abstract class Shape {
        public abstract double  Area();
    }
    class Triangle extends Shape{
        private int a,b,c;
        public Triangle() {}
        public Triangle(int a,int b,int c){
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public int getA() {
            return a;
        }
        public void setA(int a) {
            this.a = a;
        }
        public int getB() {
            return b;
        }
        public void setB(int b) {
            this.b = b;
        }
        public int getC() {
            return c;
        }
        public void setC(int c) {
            this.c = c;
        }
        public double Area() {
            double p = (a+b+c)/2;
            return  Math.sqrt(p*(p-getA())*(p-getB())*(p-getC()));
            
        }
    }
    
    
    class Rectangle extends Shape{
        private double height,width;
        
        public Rectangle() {}
        public Rectangle(double height,double width){
            this.height = height;
            this.width = 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 double Area() {
            return getHeight()*getWidth();
        }
    }
    
    
    class Circle extends Shape{
        private double radius;
        
        public Circle(){}
        public Circle(double radius) {
            this.radius = radius;
        }
        public double getRadius() {
            return radius;
        }
        public void setRadius(double radius) {
            this.radius = radius;
        }
        public double Area() {
            return Math.PI*Math.pow(getRadius(), 2);
        }
    }
    
    public class Demo2{
         public static void main(String[] args) {
             System.out.println("请输入三角形的边长:");
             Scanner s1 = new Scanner(System.in);
             int a = s1.nextInt();
             int b = s1.nextInt();
             int c = s1.nextInt();
             Triangle triangle = new Triangle(a,b,c);
             if(a+b>c && a+c>b && b+c>a) {
             System.out.println("三角形的面积为:"+triangle.Area());
             }
             else {
                 System.out.println("这不是一个三角形,无法输出面积。");
             }
             
             Scanner s2 = new Scanner(System.in);
             System.out.println("请输入矩形的长和宽:");
             double height = s2.nextDouble();
             double width = s2.nextDouble();
             Rectangle rectangle = new Rectangle(height,width);
             System.out.println("矩形的面积为:"+rectangle.Area());
             
             
             Scanner s3 = new Scanner(System.in);
             System.out.println("请输入圆的半径:");
             double radius = s3.nextDouble();
             Circle circle = new Circle(radius);
             System.out.println("圆的面积为:"+circle.Area());
             
         }
    }
    

    二)使用接口技术
    1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
    编程技巧
    (1) 接口中定义的方法在实现接口的具体类中要重写实现;
    (2) 利用接口类型的变量可引用实现该接口的类创建的对象。
    实验代码:

    package hello;
    
    import java.util.Scanner;
    
    interface Shape{
        public void Size();
    }
    class Line implements Shape{
        private double length;
        public Line() {}
        public Line(double length) {
            this.setLength(length);
        }
        
        @Override
        public void Size() {
            System.out.println("直线的长度:"+getLength());
        }
        public double getLength() {
            return length;
        }
        public void setLength(double length) {
            this.length = length;
        }
        
    }
    class Circle implements Shape{
        private double radius;
        public Circle(double radius) {
            this.radius = radius;
        }
        
        public double getRadius() {
            return radius;
        }
    
        public void setRadius(double radius) {
            this.radius = radius;
        }
        public double getArea() {
            return Math.PI*Math.pow(radius, 2);
        }
        @Override
        public void Size() {
            System.out.println("圆的面积:"+getArea());
        }
    }
    public class Demo1{
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("请输入直线的长度:");
            int length = s.nextInt();
            Line line = new Line(length);
            System.out.println("直线长度为:"+line.getLength());
            
            Scanner p = new Scanner(System.in);
            System.out.println("请输入圆的半径:");
            double radius = p.nextDouble();
            Circle circle = new Circle(radius);
            System.out.println("圆的面积为:"+circle.getArea());
        }
    }
    

    总结:
    1):抽象类不能被实例化,抽象类里可以没有抽象方法;
    2):在Java中所有的类都有一个公共的父类就是Object类,一个类只要没有明显的继承一个类,则肯定是Object类的子类;
    3):抽象类可以派生子类,在抽象类派生的子类中必须实现抽象类中定义的所有抽象方法;
    4):抽象类它本身并不能直接创建对象,只能通过继承的子类来创建对象;
    5):如果抽象类和接口都可以使用的话,优先使用接口,因为避免单继承的局限;
    6):abstract不能与final并列修饰同一个类;
    
  • 相关阅读:
    坦克大战(完结篇)
    坦克大战第一节——画出自己的坦克(新手篇)
    jq动画插件,自制基于vue的圆形时钟
    原生js数值开根算法
    html引入公共模块
    js组件
    前端不缓存,ajax不缓存,js操作cookie
    c++打印实心菱形,空心三角形,十字星,空心正方形,实心平行四边形
    css元素水平垂直居中
    vue日历/日程提醒/html5本地缓存
  • 原文地址:https://www.cnblogs.com/FLZ1208/p/11663937.html
Copyright © 2011-2022 走看看