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

    学习总结:

    本周学习了interface关键词,即接口。
    1.一个类通过继承接口的方式,从而来继承接口的抽象方法。
    2.一个接口可以有多个方法。
    3.接口没有构造方法。
    4.接口不能包含成员变量,除了 static 和 final 变量。

    (一)抽象类的使用

    设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。

    注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2

    父类

    abstract class Person {
    	private double a1;
    	public Person(double a1) {
    		this.a1 = a1;
    	}
    	
    	public double getA1() {
    		return a1;
    	}
    
    
    	public void Area() {
    		System.out.println(this.getJisuan());
    	}
    	
    	public abstract String getJisuan();
    }
    

    子类

    class yuan extends Person {
    
    	public yuan(double a1) {
    		super(a1);
    	}
    
    	public String getJisuan() {
    		return "圆形的面积=" +super.getA1()*super.getA1()*Math.PI;
    	}
    }
    
    
    class Juxing extends Person {
    	private double a2;
    	public Juxing(double a1,double a2) {
    		super(a1);
    		this.a2 = a2;
    	}
    
    	public String getJisuan() {
    		return "矩形的面积 = " +super.getA1()*this.a2;
    	}
    }
    
    
    class Sanjiaoxing extends Person {
    
    	private double a2;
    	private double a3;
    	public double p;
    	public Sanjiaoxing(double a1, double a2, double a3) {
    		super(a1);
    		this.a2 = a2;
    		this.a3 = a3;
    	}
    	
    	public double P() {
    		return p = (super.getA1()+this.a2+this.a3)/2;
    	}
    	
    	public String getJisuan() {
    		return "三角形的面积="+Math.sqrt(P()*(P()-super.getA1())*(P()-this.a2)*(P()-this.a3));
    	}
    
    }
    

    测试类

    public class wuguijun {
    
    	public static void main(String[] args) {
    		
    		Person per1 = new Juxing(6,6);
    		Person per2 = new Sanjiaoxing(3,4,5);
    		Person per3 = new yuan(1);
    		per1.Area();
    		per2.Area();
    		per3.Area();
    	}
    
    }
    

    (二)使用接口技术

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

    接口

    interface Shape{
    	public void size();
    }
    
    class w1{
    	public static void daxiao(Shape shape) {
    		shape.size();
    	}
    };
    
    
    class Yuan implements Shape{
    	public void size() {
    		System.out.println("圆的大小 = 5");
    		
    	}
    };
    
    class zhixian implements Shape{
    	public void size() {
    		System.out.println("直线的长度 = 4");
    	}
    };
    

    测试类

    public class work2 {
    
    	public static void main(String[] args) {
    		
    		w1.daxiao(new Yuan());
    		w1.daxiao(new zhixian());
    
    	}
    
    }
    

    第二题修改:

    接口

    
    interface Shape{
    
    	public void size();
    }
    
    class w1{
    	public static void daxiao(Shape shape) {
    		shape.size();
    	}
    };
    
    
    class Yuan implements Shape{
    	private double r;
    	
    	
    	public double getR() {
    		return r;
    	}
    
    
    	public Yuan(double r) {
    		this.r = r;
    	}
    
    
    	public void size() {
    		System.out.println("圆的大小 = "+r*r*Math.PI);
    		
    	}
    };
    
    class zhixian implements Shape{
    	private double n;
    	
    	public double getN() {
    		return n;
    	}
    
    	public zhixian(double n) {
    		this.n = n;
    	}	
    
    	
    	public void size() {
    		System.out.println("直线的长度 = "+n);
    	}
    };
    

    测试类

    public class work2 {
    
    	public static void main(String[] args) {
    		
    		Shape w1=new Yuan(1);
    		Shape w2=new zhixian(3);
    		
    		w1.size();
    		w2.size();
    		
    	}
    
    }
    
    

  • 相关阅读:
    WPF
    binding(转)
    C# winForm调用WebService
    如何用vs2010打开vs2013的项目?
    pyqt——布局管理
    pyqt5——对话框
    pyqt5——俄罗斯方块游戏
    pyqt5——事件和信号
    pyQT5——hello world!
    PyQt5 简介
  • 原文地址:https://www.cnblogs.com/wuguijunniubi/p/11655298.html
Copyright © 2011-2022 走看看