练习1:定义一个修饰权限为private的成员变量,并定义两个成员方法一个成员方法实现为次成员变量赋值,另一个成员方法获取这个成员变量的值,保证其他类集成该类时能获取该类的成员变量的值。
练习2:尝试编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长、宽初始化,定义一个成员方法求此矩形的面积。
练习3:根据运行参数的个数决定循环打印变量i值的次数。
1 /* 2 * 3 * Excise by http://www.cnblogs.com/WhoMercy 4 * 5 * */ 6 public class Exercise1 { 7 private static String A; 8 public static String fuzhi(){ 9 A="abcdefghijklmnopqrstuvwxyz"; 10 return A; 11 } 12 public static String quzhi(String array){ 13 String quzhi; 14 quzhi=array; 15 return quzhi; 16 } 17 public static void call(){ 18 System.out.println(Exercise1.A); 19 } 20 public static void main(String[] args){ 21 System.out.println("练习1:定义一个修饰权限为private的成员变量,并定义两个成员方法"+"\n" 22 +"一个成员方法实现为次成员变量赋值,另一个成员方法获取这个成员变量的值,"+"\n"+"保证其他类集成该类时能获取该类的成员变量的值。"); 23 System.out.println(); 24 fuzhi(); 25 quzhi(A); 26 System.out.print("A的值为: "); 27 call(); 28 System.out.print("quzhi的值为: "); 29 call(); 30 } 31 32 }
1 /* 2 * 3 * Excise by http://www.cnblogs.com/WhoMercy 4 * 5 * */ 6 public class Exercise2 { 7 public static int length; 8 private static int width; 9 public static void main(String[] args){ 10 System.out.println("练习2:尝试编写一个矩形类,将长与宽作为矩形类的属性," 11 +"\n"+"在构造方法中将长、宽初始化,定义一个成员方法求此矩形的面积。"); 12 Exercise2 m=new Exercise2(7,3); 13 System.out.println("面积是: "+m.acreage()); 14 } 15 public Exercise2(int length,int width){ 16 this.length=length; 17 this.width=width; 18 } 19 public int acreage(){ 20 int acreage = length*width; 21 return acreage; 22 } 23 24 }
1 /* 2 * 3 * Excise by http://www.cnblogs.com/WhoMercy 4 * 5 * */ 6 public class Exercise3 { 7 public static int a; 8 public static int b; 9 public static void main(String[] args){ 10 System.out.println("练习3:根据运行参数的个数决定循环打印变量i值的次数。"); 11 Exercise3 a=new Exercise3(3,4); 12 a.call(); 13 } 14 public static int number(){ 15 int number=a*b; 16 return number; 17 } 18 public Exercise3(int a,int b){ 19 this.a=a; 20 this.b=b; 21 } 22 public void call(){ 23 System.out.println("a的值为: "+this.a+"\n"+"b的值为: "+this.b); 24 System.out.println("循环打印次数(number) = a*b."); 25 for(int i=1;i<=Exercise3.number();i++){ 26 System.out.println("这是第 "+"\t"+i+"\t"+"次打印"); 27 } 28 } 29 30 }