首页 / JAVA / Java面向对象—构造器,this
Java面向对象—构造器,this
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java面向对象—构造器,this,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7598字,纯文字阅读大概需要11分钟。
内容图文
![Java面向对象—构造器,this](/upload/InfoBanner/zyjiaocheng/729/075e07b36905435b8065fc39b01b7d40.jpg)
面向对象,构造器(构造方法)用于在创建对象时初始化属性,this用来指当前类。
构造器(构造方法,Constructor)的使用
Construct:建造、建设、构造。 CCB
1.作用:创建类的对象;属性的初始化
2.说明
① 当我们在类中没有显示的定义构造器时,系统会默认提供一个空参的构造器
② 构造器的定义格式:权限修饰符 类名(形参列表){}
③ 一个类的多个构造器之间构成重载
④ 当我们在类中显示的声明了类的构造器时,系统就不再提供默认的空参构造器
⑤ 一个类中,一定会有构造器的声明。
第1题
定义一个圆形Circle类。
属性:私有化
r:半径
构造方法:
无参构造方法
满参构造方法
成员方法:
get/set方法
showArea方法:打印圆形面积
showPerimeter方法:打印圆形
代码实现:
public class Test01 { public static void main(String[] args) { Circle c1 = new Circle(1.2); c1.showArea(); c1.showPerimeter(); } } class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public Circle() { } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public void showArea(){ System.out.println("半径为:" + radius + ",面积:" + 3.14 * radius * radius); } public void showPerimeter(){ System.out.println("半径为:" + radius + ",周长:" + 2 * 3.14 * radius); } }
第2题
- 定义一个日期MyDate类。
属性:
year:年
month:月
day:日
构造方法:
满参构造方法
成员方法:
get/set方法
void showDate方法:打印日期。
boolean isLeapYear()方法:判断当前日期是否是闰年
代码实现:
public class Test02 { public static void main(String[] args) { MyDate my = new MyDate(2019,5,13); my.showDate(); boolean flag = my.isLeapYear(); System.out.println(my.getYear()+ (flag?"是闰年":"不是闰年")); } } class MyDate { private int year; private int month; private int day; public MyDate() { } public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public void showDate() { System.out.println("日期" + year + "年" + month + "月" + day + "日"); } public boolean isLeapYear() { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } }
第3题
定义一个扑克Card类。
属性:
花色
点数
- 构造方法:
满参构造方法
成员方法:
showCard方法:打印牌面信息
定义测试类,创建Card对象,调用showCard方法。
代码实现:
public class Test03 { public static void main(String[] args) { Card c = new Card("黑桃", "A"); c.showCard(); } } class Card { private String hua; private String dian; public Card(String hua, String dian) { this.hua = hua; this.dian = dian; } public Card() { } public String getHua() { return hua; } public void setHua(String hua) { this.hua = hua; } public String getDian() { return dian; } public void setDian(String dian) { this.dian = dian; } public void showCard() { System.out.println(hua + dian); } }
第4题
- 创建一个Book类,属性私有化,提供有参和无参构造,get/set
- 创建TestBook类,并创建Book数组,创建多个Book对象,存储到数组中,并遍历显示他们的信息,并按照销量从高到低排序
代码实现:
public static void main(String[] args) { Book[] all = new Book[5]; all[0] = new Book(1, "《从入门到精通》", "张三", 88, 100, 1000); all[1] = new Book(2, "《从入门到放弃》", "李四", 89, 200, 800); all[2] = new Book(3, "《从入门到脱发》", "王五", 56, 10, 500); all[3] = new Book(4, "《从入门到吃药》", "赵六", 100, 180, 900); all[4] = new Book(5, "《从入门到升仙》", "钱七", 99, 0, 1000); System.out.println("排序前:"); System.out.println("编号\t书名\t\t作者\t价格\t销量\t库存"); for (int i = 0; i < all.length; i++) { System.out.println(all[i].getInfo()); } //排序 for (int i = 1; i < all.length; i++) { for (int j = 0; j < all.length-i; j++) { if(all[j].getSales() < all[j+1].getSales()){ Book temp = all[j]; all[j] = all[j+1]; all[j+1] = temp; } } } System.out.println("排序后:"); System.out.println("编号\t书名\t\t作者\t价格\t销量\t库存"); for (int i = 0; i < all.length; i++) { System.out.println(all[i].getInfo()); } } } class Book { private Integer id; private String title; private String author; private double price; private Integer sales; private Integer stock; public Book() { } public Book(Integer id, String title, String author, double price, Integer sales, Integer stock) { this.id = id; this.title = title; this.author = author; this.price = price; this.sales = sales; this.stock = stock; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Integer getSales() { return sales; } public void setSales(Integer sales) { this.sales = sales; } public Integer getStock() { return stock; } public void setStock(Integer stock) { this.stock = stock; } public String getInfo(){ return id + "\t" + title + "\t" + author + "\t" + price + "\t" + sales + "\t" + stock; } }
第5题
声明员工类、程序员类、设计师类、架构师类
员工类属性:编号、姓名、年龄、手机号码
程序员类属性:编程语言
设计师类属性:奖金
架构师类属性:持有股票数量
要求:属性私有化,无参有参构造,get/set,getInfo方法(考虑重写)
代码实现:
public class Employee { private int id; private String name; private int age; private String tel; public Employee() { super(); } public Employee(int id, String name, int age, String tel) { super(); this.id = id; this.name = name; this.age = age; this.tel = tel; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getInfo(){ return "编号:" + name + ",姓名:" + name + ",年龄:" + age + ",电话:" + tel; } }
public class Programmer extends Employee{ private String language; public Programmer() { super(); } public Programmer(int id, String name, int age, String tel, String language) { super(id, name, age, tel); this.language = language; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } @Override public String getInfo() { return super.getInfo() + ",编程语言:" + language; } }
public class Designer extends Programmer { private double bonus; public Designer() { super(); } public Designer(int id, String name, int age, String tel, String language, double bonus) { super(id, name, age, tel, language); this.bonus = bonus; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public String getInfo() { return super.getInfo() + ",奖金:" + bonus; } }
public class Architect extends Designer { private int stock; public Architect() { super(); } public Architect(int id, String name, int age, String tel, String language, double bonus, int stock) { super(id, name, age, tel, language, bonus); this.stock = stock; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } @Override public String getInfo() { return super.getInfo() + ",持有股票数:" + stock; } }
public class Test05 { public static void main(String[] args) { Employee emp = new Employee(1, "张三", 23, "10086"); Programmer pro = new Programmer(2, "李四", 24, "10010", "java"); Designer de = new Designer(3, "王五", 25, "114", "python", 2000); Architect a = new Architect(4, "赵六", 26, "110", "java", 3000, 100); System.out.println(emp.getInfo()); System.out.println(pro.getInfo()); System.out.println(de.getInfo()); System.out.println(a.getInfo()); } }
有什么不懂的地方可以在下方留言,我会一一解答。
内容总结
以上是互联网集市为您收集整理的Java面向对象—构造器,this全部内容,希望文章能够帮你解决Java面向对象—构造器,this所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。