对象的赋值与比较

【一般是先声明在操作,这里可以先声明再创建再指向】

 1 package DemoArea6.copy;
 2 
 3 import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
 4 
 5 public class area6 {
 6     private int A;
 7     private int B;
 8     private String Color;
 9     public static int num=0;
10     
11     
12     public area6(int a,int b,String col) {
13         // 定义有参的构造方法
14         A=a;
15         B=b;
16         Color=col;
17         num++;//当构造方法被调用时num++
18         //次数的记录取决于实例化对象的语句次序,
19         //若连续实例化两个对象,则此时的num是2,若分开则会出现1、2
20     }
21     
22     int showarea(){
23         return A*B;
24     }
25     String showcolor(){
26         return Color;
27     }
28     public static void count(){
29         //声明count是静态方法
30         
31         System.out.println("访问了"+num+"次");
32         
33     }
34 }
 1 package DemoArea6.copy;
 2 
 3 public class Mainarea6 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7          area6 a1,a2;//声明两个引用型变量
 8         
 9          a1=new area6(3,12,"hS");// 创建对象并指向
10          a2=new area6(3,6,"LS");
11         
12         System.out.println("A1"+a1.showarea());
13         System.out.println("A1"+a1.showcolor());
14         a1.count();//用实例化对象调用,可
15         
16         //area5 a2=new area5(3,6,"了LS");// 第二次调用
17         System.out.println("A2"+a2.showarea());
18         System.out.println("A2"+a2.showcolor());
19         a2.count();
20         
21         
22     }
23 
24 }

猜你喜欢

转载自www.cnblogs.com/dede-6/p/11899813.html
今日推荐