【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()) ; } }

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】