• 30.编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类E,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。


    package zuoye8;
    
    public abstract class Shape {
    	
    	private double zhouchang ;
    	private double mianji ;
    	
    	
    	public Shape(double zhouchang, double mianji) {
    		super();
    		this.zhouchang = zhouchang;
    		this.mianji = mianji;
    		
    	}
    
    	public Shape() {
    		super();
    	}
    	//抽象一个周长方法
    	 public  double zhouchang ()
    	 {
    		return zhouchang;}
    	 
    	
    
    }
    

      

    package zuoye8;
    
    public class Juxing extends Shape {
    	
    	private double c;
    	private double d;
    
    	public Juxing( double c, double d) {
    		super();
    		this.c = c;
    		this.d = d;
    		
    	}
    
    	@Override
    	public double zhouchang() {
    		double zhouchang ;
    		zhouchang =(c+d)*2;
    		return zhouchang ;
    	}
    
    }
    

      

    package zuoye8;
    
    public class Sanjaio extends Shape {
    	//三角形三边属性
    	private double a;
    	private double b;
    	private double c;
    	
    	
    	public Sanjaio( double a, double b, double c) {
    		super();
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    	public Sanjaio() {
    		
    	}
    
    	//周长
    	
    	
    	 public void
    	 zhouchang (double a,double b,double c)
    	 {
    		 if(a+b>c&&Math.abs(a-b)<c)//绝对值,保证是正整数
    			{
    			 double zhouchang ;
    			 System.out.println(zhouchang =a+b+c);
    			 
    			}
    			else
    			{
    				System.out.println("这不是三角形");
    				
    			}
    		 
    	 }
    	
    	
    
    }
    

      

    package zuoye8;
    
    public class E {
    
    	public static void main(String[] args) {
    	
    		Juxing j=new Juxing(3,4);
    		System.out.println("长=4,宽=3的矩形周长="+j.zhouchang());
    	
    		Sanjaio s=new Sanjaio();
    		System.out.println( "边分别是3,4,5的三边形周长是:");
    		s.zhouchang(3, 4, 5);
    		
    		s.zhouchang(3,1,5);
    
    
    	}
    
    }
    

      

  • 相关阅读:
    8-21模拟赛解题报告
    8-20模拟赛解题报告
    8-19模拟赛解题报告
    8-18模拟赛解题报告
    8-27复习(写题)报告
    [省赛训练(DP)]Course Selection System
    Trie(字典树)的基本操作与应用(一般与字符串前缀相关)
    [算法学习]欧拉筛
    构造函数运行的机制
    js基本数据类型之间的转换
  • 原文地址:https://www.cnblogs.com/liuyanzeng/p/5897820.html
Copyright © 2020-2023  润新知