• 第七周课程总结及实验报告


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

    2.编程技巧

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

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

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

    1)实验代码

    package text6;
    
    public abstract class Shape {
        
        public void Area() {
            System.out.println(this.getArea());
        }
        public abstract String getArea();
        
    }
    
    package text6;
    
    public class Triangle extends Shape {
        private double a,b,c;
    
        public Triangle(double a, double b, double c) {
            super();
            this.a = a;
            this.b = b;
            this.c = c;
        }
        
        public String getArea() {
            double p=(a+b+c)/2;
            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;
        }
        
    
    }
    
    package text6;
    
    public class Rectangle extends Shape {
        
        private double length;
        private double width;
        
        public double getLength() {
            return length;
        }
        public void setLength(double length) {
            this.length = length;
        }
        public double getWidth() {
            return width;
        }
        public void setWide(double width) {
            this.width = width;
        }
        
        public Rectangle(double length, double width) {
            super();
            this.length = length;
            this.width = width;
        }
        
        public String getArea() {
            return "矩形面积   "+this.width*this.length;
        }
        
    }
    
    
    package text6;
    
    public class Circle extends Shape{
        
        private double r;
    
        public Circle(double r) {
            super();
            this.r = r;
        }
        
        public String getArea() {
            return "圆面积   "+r*r*3.1415926;
        }
    
    }
    
    package text6;
    
    public class Text6 {
    
        public static void main(String[] args) {
            Shape s1=new Triangle(3,4,5);
            Shape s2=new Rectangle(5,7);
            Shape s3=new Circle(7);
            s1.Area();
            s2.Area();
            s3.Area();
    
        }
    
    }
    

    2)运行截图

    二)使用接口技术

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

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

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

    1)实验代码

    package text6;
    
    public interface Shape {
        
        public void size();
        
    }
    
    package text6;
    
    public class Circle implements Shape {
        
        private double r;
        
        public double getR() {
            return r;
        }
    
        public void setR(double r) {
            this.r = r;
        }
        
        public Circle(double r) {
            super();
            this.r = r;
        }
    
        public void size() {
            
            System.out.println("圆大小: "+r*r*3.14);
            
        }
    
    }
    
    package text6;
    
    public class Line implements Shape {
        private double l;
    
        public double getL() {
            return l;
        }
    
        
        public Line(double l) {
            super();
            this.l = l;
        }
    
    
        public void setL(double l) {
            this.l = l;
        }
    
    
        public void size() {
            
            System.out.println("直线大小:"+l);
        }
        
    
    }
    
    package text6;
    
    public class Text6 {
    
        public static void main(String args[]) {
            Shape s1=new Circle(3);
            Shape s2=new Line(7);
            s1.size();
            s2.size();
        }
    
    }
    

    课程总结
    这周主要是学了抽象类和接口
    对象 都通过对象的多态性产生实例化对象 都通过对象的多态性产生实例化对象
    局限 抽象类有单继承的局限 接口没有单继承的局限
    实际 作为一个模板 是作为一个标准或是表示一种能力
    选择 如果抽象类和接口都可以使用的话,优先使用接口,因为避免单继承的局限 如果抽象类和接口都可以使用的话,优先使用接口,因为避免单继承的局限
    特殊 一个抽象类中可以包含多个接口,一个接口中可以包含多个抽象类 一个抽象类中可以包含多个接口,一个接口中可以包含多个抽象类

  • 相关阅读:
    最近ACM刷题见到的没见过的名词
    最近刷题常常遇见的需要百度的知识点
    简单RPG场景实现(改
    简单RPG场景实现
    位图载入与位图移动
    动态菜单
    静态菜单
    双人五子棋
    HDU 2112 HDU Today
    音痴又音痴的LT
  • 原文地址:https://www.cnblogs.com/zuoshuai/p/11663180.html
Copyright © 2020-2023  润新知