定义一个矩形类Rectangle:(知识点:对象的创建和使用)
1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2 有2个属性:长length、宽width
3 创建一个Rectangle对象,并输出相关信息
package huaerzi; public class huasunzi { int length; int width; public void getArea(){ System.out.println(length*width); } public void getPer(){ System.out.println(2*(length+width)); } public void showAll(){ System.out.println("CHANG"+length); System.out.println("KUAN"+width); System.out.println("S"); getArea(); System.out.println("C"); getPer(); } }
package huaerzi; public class huasunzi { int length; int width; public void getArea(){ System.out.println(length*width); } public void getPer(){ System.out.println(2*(length+width)); } public void showAll(){ System.out.println("CHANG"+length); System.out.println("KUAN"+width); System.out.println("S"); getArea(); System.out.println("C"); getPer(); } }
package huaerzi; public class huasunzi{ public static void main(String[] args) { // TODO Auto-generated method stub Rectangle p=new Rectangle(); p.length=4; p.width=1; p.showAll(); } }