Tuesday, December 20, 2011

Constructor dan Setter Getter

--->Program menggunakan Constructor
Buat class namanya Person

public class Person{
     private String name;

     public Person(String name){
     this.name=name;    
     }
     public String ambilNama(){
     return name;
     }
}

Class Main

public static void main(String[] args) {
    
        Person alice = new Person("Alice");
        Person bob = new Person("Bob");
       
        System.out.println(alice.ambilNama());
        System.out.println(bob.ambilNama());
    }

--->Program menggunakan Setter dan Getter
Buat class namanya Person

public class Person{
private String name;

     public void setName(String name){
     this.name=name
     }
     public getName(){
     return name;
     }
}

Class Main

public static void main(String[] args) {
    
        Person alice = new Person();
        Person bob = new Person();        
        alice.setName("alice");
        alice.setName("bob");
        System.out.println(alice.getName());
        System.out.println(bob.getName());
    }

No comments:

^_^

SEMOGA BERMANFAAT