14周博客写作

接口的多重继承

package com.company;
interface Face4{
    
    
    static final double PI=3.14;
    abstract double area();
}
interface Face5{
    
    
    abstract double volume();
}
class Cylinder1 implements Face4,Face5{
    
    
    private double radius;
    private int height;
    public Cylinder1(double radius,int height){
    
    
        this.radius=radius;
        this.height=height;
    }
    public double area(){
    
    
        return PI*radius*radius;
    }
    public double volume(){
    
    
        return area()*height;
    }
}
public class lots {
    
    
    public static void main(String[] args) {
    
    
        Cylinder1 s =new Cylinder1(5.0,2);
        System.out.println("圆柱的体积为"+s.volume());
    }
}

抽象类的应用

package com.company;
abstract class Shape{
    
    
    protected String name;
    public Shape(String name){
    
    
        this.name=name;
        System.out.println("名字:"+name);
    }
    abstract public double getArea();
    abstract public double getLength();
}
class Circle extends Shape{
    
    
    private final double pi = 3.14;
    private double radius;
    public Circle(String shapeName,double radius){
    
    
        super(shapeName);
        this.radius=radius;
    }
    public double getArea(){
    
    
        return pi*radius*radius;
    }
    public double getLength(){
    
    
        return 2*pi*radius;
    }
}
class Rectangle extends Shape{
    
    
    private double width;
    private double height;
    public Rectangle(String shapeName,double width,double height){
    
    
        super(shapeName);
        this.width=width;
        this.height=height;
    }
    public double getArea(){
    
    
        return width*height;
    }
    public double getLength(){
    
    
        return 2*(width+height);
    }
}
public class chouxiangtest {
    
    

    public static void main(String[] args) {
    
    
        Shape s =new Circle("圆",10.2);
        System.out.print("圆的面积为"+s.getArea());
        System.out.println("圆的周长为"+s.getLength());
        Shape s1 = new Rectangle("矩形",6.5,10.5);
        System.out.print("矩形的面积为"+s1.getArea());
        System.out.print("矩形的周长为"+s1.getLength());
    }
}

对接口的简单初始应用

package com.company;
interface Ishape{
    
    
    static final double pi = 3.14;
    abstract double getArea();
    abstract double getLength();
}
class Cricle implements Ishape{
    
    
    double radius;
    public Cricle(double radius){
    
    
        this.radius=radius;
    }
    public double getArea(){
    
    
        return pi*radius*radius;
    }
    public double getLength(){
    
    
        return 2*pi*radius;
    }
}
class Rectangle implements Ishape{
    
    
    private double width;
    private double height;
    public Rectangle(double width,double height){
    
    
        this.width=width;
        this.height=height;
    }

    @Override
    public double getArea() {
    
    
        return width*height;
    }

    @Override
    public double getLength() {
    
    
        return 2*(width+height);
    }
}
public class Main {
    
    

    public static void main(String[] args) {
    
    
	    Ishape s =new Cricle(5.0);
        System.out.print("圆面积"+s.getArea());
        System.out.println("圆周长"+s.getLength());
        Rectangle s1 =new Rectangle(6.5,10.8);
        System.out.print("矩形面积"+s1.getArea());
        System.out.println("矩形周长"+s1.getLength());
    }
}

猜你喜欢

转载自blog.csdn.net/m0_52040370/article/details/110732326
今日推荐