Java学习笔记 --抽象类与接口 基于类Shape

//先写一个抽象类 
import java.awt.print.Printable;


public abstract class Polygon {
	private int num;
	public Polygon(int a) {
		// TODO Auto-generated constructor stub
		num = a;
	}
	public void Printable(){
		System.out.println("num:"+num);
	}
	
}


再写一个接口 

public interface Shape {
	void area();
	void permitate();
}


再写出正方形类去继承抽象类 实现接口


import java.awt.geom.Area;

public class Square extends Polygon implements Shape {
	private double length;

	public Square(int a) {
		super(a);
		// TODO Auto-generated constructor stub
		length = a;
	}
	public void area()
	{
		System.out.println("area = " + length*length);
	}
	public void permitate()
	{
		System.out.println("permitate = "+4*length);
	}

}


最后写一个测试类 作为程序的入口


public class test {
	public static void main(String args[]){
		Square a = new Square(3);
		a.area();
		a.permitate();
		
	}
}




你可能感兴趣的:(Java学习笔记 --抽象类与接口 基于类Shape)