구조 방법 (자바 에서 유사 명 과 같은 방법 명 역할)

 Java ,                 .
Java                  :    (constructor)
              ,              :  
1.                          ,   void   。  
2.                  new     。             。  
3.               .  
4.   static、final、synchronized、abstract native  。           。  
5.              ,           .            。                 ,        ,            ,                  ,          。   
       :     、  20、10 6、3         。   
class RectConstructor{  
double length;   
double width;   
double area(){   
return length*width;   }   
RectConstructor(double width,double length){//           
this.length=length;   this.width=width;  
 }   
}   
public class RectDemo{   
public static void main(String args[])
 {   RectConstructor rect1=new RectConstructor(10,20);   
RectConstructor rect2=new RectConstructor(3,6);  
 double ar;   ar=rect1.area();  
 System.out.println("          "+ar);  
 ar=rect2.area();   System.out.println("          "+ar); 
  }   
}

좋은 웹페이지 즐겨찾기