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()) ; } }
转载于:https://www.cnblogs.com/zhaocundang/p/4887999.html
