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

    (一)设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
    注:三角形面积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接口的工作原理⑧。

  • 相关阅读:
    Can't connect to X11 window server using 的问题,求解
    自动化运维,让你远离背锅侠
    python netmiko实现cisco网络设备配置备份
    如何备份思科、锐捷、Juniper的配置文件
    网络配置备份。
    使用SecureCRT脚本备份网络设备配置的一点感悟
    網管利器!開源管理系統-LibreNMS
    邮件协议与port
    视频播放技术汇总(列表播放,小窗播放,跨界面播放,播放中网络切换提示)
    android插件式开发资料整理
  • 原文地址:https://www.cnblogs.com/swaggy89/p/11663284.html
Copyright © 2011-2022 走看看