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


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

    package shiyan4;
    
    abstract class shape {
        public abstract double print();   
    }
    class s extends shape { 
    	private double a;
        private double b;
        private double c;         
        public s(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 j extends shape {
        private double width;
        private double height; 
        public j(double width, double height){
            this.height=height;this.width=width;    
        }
        public double print() {      
            return width*height;  
        }
    }
    class y extends shape {
    	double banjing;     
        public y(double banjing){  
            this.banjing=banjing;  
        }
        public double print() {     
            return Math.PI*banjing*banjing;
        }
    }
    public class shiyan4 {
        public static void main(String[] args){
            shape s1=new s(3,6,8);
            shape s2=new j(5,10);
            shape s3=new y(5);     
            System.out.println("三角形的面积为: "+s1.print()+"矩形的面积为: "+s2.print()+"圆的面积为: "+s3.print());     
        }
    }
    

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

    package shiyan4;
    
    public interface Shape {
    	public abstract void size(); 
    }
    class z implements Shape{   
    	private double x;
    	public z(double x){  
    		this.x=x;     
    	}
    	public void size() {  
    	    System.out.println("直线: "+x);
    	}
    }
    class y implements Shape{
    	private double banjing;
    	public y(double banjing){    
    	    this.banjing=banjing;        
    	}
    	public void size(){    
    	    System.out.println("圆的面积: "+Math.PI*banjing*banjing);
    	}
    }
    package shiyan4;
    
    public class shiyan {
    	public static void main(String[] args){
            Shape s1=new z(10);
            Shape s2=new y(10);   
            s1.size();
            s2.size();
    	}
    }
    
    

    本周学习总结
    学习了新的关键词object,并对其有了一些了解。
    学了抽象类和接口,就是usb接口的工作原理⑧。

  • 相关阅读:
    如何配置tomcat的环境变量
    如何配置jdk的本地环境
    如何下载最新版本和旧版本的eclipse?
    用eclipse开发需要准备什么?
    eclipse.ini相关问题
    jquery如何遍历table,并对table里的某一个单元格进行操作
    jquery的Layer弹出框操作
    select列表遍历和触发事件
    Spring AOP详细介绍
    简单介绍Spring的ContextLoaderListener
  • 原文地址:https://www.cnblogs.com/swaggy89/p/11663284.html
Copyright © 2020-2023  润新知