【JAVA】调用类中的属性

class person
{
    String name;
    int age;
    String like;
    
    void setName(String name)
    {
        this.name = name;
    }
    
    void setAge(int age)
    {
        this.age =age;
    }
    
    void setLike(String like)
    {
        this.like=like;
    }
    
    void talk()
    {
        System.out.println( "我是:" + name + ",今年:" + age + "岁," + "喜欢:" + like );
    }

}

public class classwork
{
    public static void main( String[] args ) 
    {
        person p = new person();
        p.setName( "小明");
        p.setAge(20);
        p.setLike("音乐");
        
        p.talk();
    }
}

 

class Book  
{   
    String title ;  
    double price ;  
    public void printInfo()  // 书籍信息
    {  
        System.out.println("书的名字:" + title + ",价格:" + price) ;
    }
}


public class work2
{
    public static void main(String args[]) {
        Book bookA = new Book() ;   
        Book bookB = new Book() ;
        bookA.title = "程序设计" ; 
        bookA.price = 40.7 ;       
        bookB.title = "C语言" ;
        bookB.price = 33.9 ;
        bookB = bookA ;            
        bookA.printInfo() ;
        bookB.printInfo();
        
    }
}

 

class Books 
{
    private String title; 
    private double price ;
    String pub = "天天精彩出版社" ;    
    public Books(String title,double price) 
    {
        this.title = title ;
        this.price = price ;
    }  
    public String getInfo() 
    {
        return "图书名称:" + this.title + ",价格:" + this.price + "元,出版社:" + this.pub ;
    }
}
public class work3
{
    public static void main(String args[]) 
    {
        Books b1 = new Books("Java开发实战经典",59.8) ;
        Books b2 = new Books("Java WEB开发实战经典(基础篇)",49.9) ;
        Books b3 = new Books("Android开发实战经典",68) ;
        System.out.println(b1.getInfo()) ;
        System.out.println(b2.getInfo()) ;
        System.out.println(b3.getInfo()) ;
      
        System.out.println("----------------------出版社改名之后-------------------------") ;
        b1.pub = "每日精彩出版社" ;
        b2.pub = "每日精彩出版社" ;
        b3.pub = "每日精彩出版社" ;
        System.out.println(b1.getInfo()) ;
        System.out.println(b2.getInfo()) ;
        System.out.println(b3.getInfo()) ;
    }
}

 

posted @ 2015-10-17 18:59  zhaocundang  阅读(1735)  评论(0编辑  收藏  举报