Jave:接口
Shape
package com.wanson.HandleInterface;
/**
* 接口就是一种公共的规范标准。
* 只要符合规范标准,就可以大家通用。
* 接口interface一种引用数据类型,其中抽象方法是关注要点
*/
public interface Shape {
// 以下都是抽象方法
/* public abstract void methodAbs1();
abstract void methodAbs2();
public void methodAbs3();
void methodAbs4();*/
public double getArea();
public double getCircumference();
// 默认方法 :目的是解决 接口升级的问题
public default void getInfomation(){
System.out.println("这是新添加方法");
}
public static void getVIPInformation(){
System.out.println("VIP信息");
}
}
Animals
package com.wanson.HandleInterface;
public interface Animals {
// 接口默认方法
public default void getAnimalName(){
System.out.println("动物品种");
printStart();
}
// 接口静态方法
public static void getExtraFullInfomation(){
System.out.println("获取额外信息");
printStart();
}
private static void printStart(){
for (int i = 0; i <50 ; i++) {
System.out.print("\t"+"*");
}
}
}
Cat
package com.wanson.Cat;
import com.wanson.HandleInterface.Animals;
import com.wanson.HandleInterface.Shape;
public class Cat implements Animals {
}
Rectangle
package com.wanson.Rectangle;
import com.wanson.HandleInterface.Shape;
public class Rectangle implements Shape {
private double width;
private double height;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
System.out.println("面积信息:\t");
return width*height;
}
public double getCircumference() {
System.out.println("周长信息:\t");
return (width+height)*2;
}
@Override
public void getInfomation() {
System.out.println("正方形");
}
}
测试
package com.wanson.HandleInterface.test;
import com.wanson.Cat.Cat;
import com.wanson.HandleInterface.Animals;
import com.wanson.HandleInterface.Shape;
import com.wanson.Rectangle.Rectangle;
public class Driver {
public static void main(String[] args) {
Cat cat=new Cat();
cat.getAnimalName();
Rectangle rectangle=new Rectangle();
System.out.println(rectangle.getArea());
System.out.println(rectangle.getCircumference());
rectangle.getInfomation();
Shape.getVIPInformation();
Animals.getExtraFullInfomation();
}
}
posted on 2019-05-10 19:42 Indian_Mysore 阅读(166) 评论(0) 收藏 举报
浙公网安备 33010602011771号