编程知识 cdmana.com

Java method review

{tabs-pane label=" Method "} public Exposure to the outside may cause Break encapsulation public String name2; private Modifier Deny external access ; private String nam1;

public static void main(String[] args){
        // Method 
        //ff1  private
        ff1 m=new ff1();
        m.setBirth(2005);
        System.out.println(m.getAge1());
        m.getList("123", 18);
        System.out.println(m.getName()+","+m.getAge());
        
    }
       // Method 1 ff1
class ff1{
   private String name;
   private int age;
   private int birth;

   public void setBirth(int birth) {
       this.birth = birth;
   }
   // private Method :
   private int calcAge(int currentYear) {
       return currentYear - this.birth;
   }
   public String getName() {
       return this.name;
   }
   public void setName(String name) {
       if(name==null || name.isEmpty()) {
           throw new IllegalArgumentException(" Report errors ");
       }   
       this.name = name;
   }
   public int getAge1() {
        return calcAge(2019); //  call private Method 
   }
   public int getAge(){
       return this.age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public void getList(String name,int age){
       if(name==null || name.isEmpty() || String.valueOf(age).equals("")){
           throw new IllegalArgumentException(" Report errors ");
       }   
       this.name=name;
       this.age=age;
   }
}

{/tabs-pane} {tabs-pane label=" effect "}

{/tabs-pane}

This article altogether 42 Number of words , Average reading time ≈ 1 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824204p.html

Scroll to Top