Compare commits
3 Commits
db5d908e6c
...
c4b089a048
| Author | SHA1 | Date | |
|---|---|---|---|
| c4b089a048 | |||
| 8526e31417 | |||
| 70585d8d65 |
53
day06/src/com/inmind/constructor03/Cat.java
Normal file
53
day06/src/com/inmind/constructor03/Cat.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package com.inmind.constructor03;
|
||||||
|
|
||||||
|
public class Cat {
|
||||||
|
//属性
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private String color;
|
||||||
|
|
||||||
|
//构造方法
|
||||||
|
//alt+insert
|
||||||
|
public Cat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cat(String name, int age, String color) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cat(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
//getter和setter方法
|
||||||
|
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 getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColor(String color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
//行为
|
||||||
|
public void catchMouse() {
|
||||||
|
System.out.println("抓老鼠");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
day06/src/com/inmind/constructor03/Demo05.java
Normal file
16
day06/src/com/inmind/constructor03/Demo05.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.constructor03;
|
||||||
|
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student s = new Student();
|
||||||
|
System.out.println(s);
|
||||||
|
System.out.println(s.getAge());
|
||||||
|
System.out.println(s.getName());
|
||||||
|
|
||||||
|
|
||||||
|
Student s1 = new Student("王五", 22);
|
||||||
|
System.out.println(s1.getAge());//22
|
||||||
|
System.out.println(s1.getName());//王五
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
72
day06/src/com/inmind/constructor03/Student.java
Normal file
72
day06/src/com/inmind/constructor03/Student.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package com.inmind.constructor03;
|
||||||
|
/*
|
||||||
|
Student():就是Student类的"默认"无参构造方法
|
||||||
|
|
||||||
|
普通自定义方法:
|
||||||
|
方法修饰符 返回值类型 方法名(参数列表){
|
||||||
|
return;方法体
|
||||||
|
}
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
方法修饰符 类名(参数列表){
|
||||||
|
java方法体
|
||||||
|
}
|
||||||
|
|
||||||
|
注意:当源文件(.java文件),进行编译时,编译器扫描整个类的内容,如果没有发现构造方法,那么它会自动帮你添加一个
|
||||||
|
默认无参构造方法,如果你写了构造方法,编译器就不会自动添加默认无参构造方法
|
||||||
|
|
||||||
|
1.构造方法没有返回值类型
|
||||||
|
2.构造方法必须与类名保持一致
|
||||||
|
3.构造方法可以重载
|
||||||
|
|
||||||
|
构造方法的作用:通过new调用构造方法,创建对象,并且对该对象的属性进行赋值
|
||||||
|
*/
|
||||||
|
public class Student {
|
||||||
|
//属性
|
||||||
|
//姓名
|
||||||
|
private String name;
|
||||||
|
//年龄
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
//无参构造方法
|
||||||
|
public Student(){
|
||||||
|
System.out.println("无参构造方法");
|
||||||
|
}
|
||||||
|
//有参构造
|
||||||
|
public Student(String name){
|
||||||
|
System.out.println("有参构造方法");
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
//有参构造
|
||||||
|
public Student(int age){
|
||||||
|
System.out.println("有参构造方法");
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
//满参构造方法
|
||||||
|
public Student(String name, int age){
|
||||||
|
System.out.println("满参构造方法");
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//行为
|
||||||
|
//get方法
|
||||||
|
public String getName(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//get方法
|
||||||
|
public int getAge(){
|
||||||
|
return this.age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//学习
|
||||||
|
public void study(String book){
|
||||||
|
System.out.println(this.name+"学生正在学习" + book);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
day06/src/com/inmind/test04/Book.java
Normal file
61
day06/src/com/inmind/test04/Book.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package com.inmind.test04;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
//属性
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
private String publishDate;
|
||||||
|
|
||||||
|
//无参构造方法
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//满参构造方法
|
||||||
|
public Book(String id, String name, double price, String publishDate) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
//get/set方法
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublishDate() {
|
||||||
|
return publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublishDate(String publishDate) {
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
//自定义showBook方法
|
||||||
|
public void showBook() {
|
||||||
|
System.out.println("图书编号:" + this.id + ",图书名称:" + this.name + ",图书价格:"
|
||||||
|
+ this.price + ",出版日期:" + this.publishDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
53
day06/src/com/inmind/test04/Test06.java
Normal file
53
day06/src/com/inmind/test04/Test06.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package com.inmind.test04;
|
||||||
|
|
||||||
|
import com.inmind.constructor03.Cat;
|
||||||
|
import com.inmind.private02.Student;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 定义一个图书Book类。
|
||||||
|
|
||||||
|
- 属性:图书编号,书名,价格,出版日期
|
||||||
|
- 构造方法:
|
||||||
|
- 无参构造方法,满参构造方法
|
||||||
|
- 成员方法:
|
||||||
|
- get/set方法
|
||||||
|
- showBook方法,输出图书信息
|
||||||
|
|
||||||
|
* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
||||||
|
*/
|
||||||
|
public class Test06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建三个图书对象
|
||||||
|
Book b1 = new Book("b001", "java从入门到入土", 9.0, "2026-01-01");
|
||||||
|
Book b2 = new Book("b003", "mysql从删库到跑路", 99.0, "2026-02-01");
|
||||||
|
Book b3 = new Book("b005", "Linux操作系统", 9.9, "2026-01-01");
|
||||||
|
|
||||||
|
|
||||||
|
//定义一个数组,存放图书对象
|
||||||
|
/*Book[] books = new Book[3];//动态初始化,创建一个长度为3的存放book对象的数组
|
||||||
|
books[0] = b1;
|
||||||
|
books[1] = b2;
|
||||||
|
books[2] = b3;*/
|
||||||
|
//Book[] books = new Book[]{b1,b2,b3};//静态初始化
|
||||||
|
Book[] books = {b1,b2,b3};//静态初始化简写形式
|
||||||
|
|
||||||
|
//可以找出最贵的书对象&&也可以直接找出最贵的索引(遍历操作||if-else)
|
||||||
|
|
||||||
|
//定义出一个变量,记录最贵的书对象
|
||||||
|
// Book maxPriceBook = books[0];
|
||||||
|
int maxIndex = 0;
|
||||||
|
|
||||||
|
//遍历判断,找出最贵的书对象
|
||||||
|
for (int i = 1; i < books.length; i++) {
|
||||||
|
//根据索引获取当前遍历的元素
|
||||||
|
Book book = books[i];
|
||||||
|
Book maxBook = books[maxIndex];
|
||||||
|
if (book.getPrice() > maxBook.getPrice()) {
|
||||||
|
maxIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//输出最贵图书信息。
|
||||||
|
// maxPriceBook.showBook();
|
||||||
|
books[maxIndex].showBook();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user