标签:
public class juxing { //编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长、宽初始化,定义一个成员方法求此矩形面积 int chang; int kuan; int mianji; double s; double c; int n = 4; String tuxing = "矩形"; void xingzhuang(){ //无返回无参数 tuxing = "矩形"; } void djx(double m){ //无返回值,有参数 ???? m = c-2; System.out.println("对角线 = " + m); } double mianji(int chang,int kuan){ //有返回值,有参数 return mianji= chang*kuan; } boolean bianshu(){ //有返回值,无参数 boolean b = true; if (n == 4){ System.out.println("有四条边 " + b); }else{ System.out.println("不是四边形"); } return b; } public static void main (String[] args){ juxing jx = new juxing(); jx.chang = 10; jx.kuan = 5; jx.s = jx.chang*jx.kuan; jx.mianji = jx.chang * jx.kuan; jx.c = 2 * (jx.chang + jx.kuan); jx.djx(jx.c); System.out.println("图形:"+jx.tuxing); System.out.println("面积= "+jx.s); System.out.println("矩形的面积= "+jx.mianji); System.out.println("周长 = "+jx.c); //System.out.println("对角线 = "+jx.m); if (jx.bianshu()){ System.out.println("这个图形是四边形"); }else{ System.out.println("这个图形不是四边形"); } } }
0117 面向对象OOP有关方法、类、构造方法及权限修饰符的练习
标签:
原文地址:http://www.cnblogs.com/sjxx/p/5137447.html