Object-oriented, constructors (constructors) are used to initialize properties when creating objects, and this is used to refer to the current class.
Use of Constructors
Construct: Construction, construction, construction. CCB
1. Role: Create objects of classes; Initialization of attributes
2. Description
When we define constructors that are not shown in the class, the system defaults to providing an empty parameter constructor
(2) Definition format of constructor: permission modifier class name (parametric list) {}
(3) Multiple constructors of a class constitute overload
(4) When we declare the constructor of a class as shown in the class, the system no longer provides the default empty parameter constructor.
_In a class, there must be a constructor declaration.
Topic 1
-
Define a circular Circle class.
-
Attribute: Privatization
-
r: Radius
-
-
Construction method:
-
Parametric-free construction method
-
Full-parameter Construction Method
-
-
Membership methods:
-
get/set method
-
showArea Method: Print Circle Area
-
showPerimeter Method: Print Circle
-
-
Code implementation:
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:"+radius + area:"+3.14 * radius * radius); } public void showPerimeter(){ System.out.println("radius:"+radius + perimeter: "+2 * 3.14 * radius); } }
Question 2
- Define a date MyDate Class.
Attributes:
year: year
month: month
day: day
Construction method:
Full-parameter Construction Method
Membership methods:
get/set Method
void showDate Method: Print date.
boolean isLeapYear()Method: To determine whether the current date is a leap year or not.
Code implementation:
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?"It's a leap year.":"Not a leap year")); } } 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("date" + year + "year" + month + "month" + day + "day"); } public boolean isLeapYear() { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } }
Question 3
-
Define a poker Card class.
-
Attributes:
-
Flower color
-
Point
-
- Construction method:
-
Full-parameter Construction Method
-
Membership methods:
-
showCard Method: Printing Plate Information
-
-
-
Define test class, create Card object, and call showCard method.
Code implementation:
public class Test03 { public static void main(String[] args) { Card c = new Card("spades", "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); } }
Question 4
- Create a Book Class, property privatization, providing parametric and nonparametric constructs, get/set
- Establish TestBook Class, and create Book Array, create multiple Book Objects, stored in arrays, and traversed to display their information, sorted by volume from high to low
Code implementation:
public static void main(String[] args) { Book[] all = new Book[5]; all[0] = new Book(1, "<From Introduction to Proficiency", "Zhang San", 88, 100, 1000); all[1] = new Book(2, "<From Initial to Abandoned", "Li Si", 89, 200, 800); all[2] = new Book(3, "<From Introduction to Hair Loss", "Wang Wu", 56, 10, 500); all[3] = new Book(4, "<From Initial to Drug-taking ____________", "Zhao Liu", 100, 180, 900); all[4] = new Book(5, "<From Introduction to Immortality", "Money seven", 99, 0, 1000); System.out.println("Before sorting:"); System.out.println("number\t Title\t\t author\t Price\t Sales volume\t Stock"); for (int i = 0; i < all.length; i++) { System.out.println(all[i].getInfo()); } //sort 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("After sorting:"); System.out.println("number\t Title\t\t author\t Price\t Sales volume\t Stock"); 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; } }
Topic 5
Statement of staff, programmers, designers, Architects
-
Employee Category Attributes: Number, Name, Age, Mobile Number
-
Programmer class attributes: programming languages
-
Designer class attributes: bonuses
-
Architect class attribute: number of shares held
-
Requirements: Property privatization, no parametric construction, get/set, getInfo method (considering rewriting)
Code implementation:
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 "Number:" + name + ", name:" + name + ", age:" + age + ", telephone:" + 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() + ",Programing language:" + 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:" + 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() + ",Number of shares held:" + stock; } }
public class Test05 { public static void main(String[] args) { Employee emp = new Employee(1, "Zhang San", 23, "10086"); Programmer pro = new Programmer(2, "Li Si", 24, "10010", "java"); Designer de = new Designer(3, "Wang Wu", 25, "114", "python", 2000); Architect a = new Architect(4, "Zhao Liu", 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()); } }
Anything you don't know can be left below, I will answer them one by one.