02 Java面向对象—第3节 面向对象进阶

一、面向对象进阶训练任务

1、编写一个类 Book,代表图书

问题描述

具有属性: 名称(title)、页数(pageNum),其中页数不能少于 200 页,否则输出错误信息,并赋予默认值 200。

具有方法: 为各属性设置赋值和取值方法。 detail,用来在控制台输出每 本图书的名称和页数

编写测试类 BookTest 进行测试:为 Book 对象的属性赋予初始值,并调 用 Book 对象的 detail 方法,看看输出是否正确

代码

public class BookTest {
    
    

	public static void main(String[] args) {
    
    
		
		Book book1 = new Book();
		book1.setTitle("《Java编程思想》");
		book1.setPageNum(150);//错误提示
		book1.detail();
		
		Book book2 = new Book("《Java进阶》",680);
		book2.detail();
	}
}

class Book {
    
    
	private String title;
	private int pageNum;
	
	Book(){
    
    
		
	}
	
	Book(String title,int pageNum){
    
    
		this.title = title;
		this.pageNum = pageNum;
	}
	
	public String getTitle() {
    
    
		return title;
	}
	public void setTitle(String title) {
    
    
		this.title = title;
	}
	
	public int getPageNum() {
    
    
		return pageNum;
	}
	public void setPageNum(int pageNum) {
    
    
		if(pageNum >= 200) {
    
    
			this.pageNum = pageNum;
		}else {
    
    
			System.out.println("页数不能少于200,已自动设置为默认值200");//输出提示
			this.pageNum = 200;
		}
	}
	
	public void detail() {
    
    
		System.out.println("图书名称为" + title + ",页数为" + pageNum);
	}
}

运行结果:
在这里插入图片描述

2、通过类描述开课吧的 Java 学员

问题描述

具有属性: 姓名,年龄,性别,爱好,公司(都是:开课吧),学科(都 是:Java 学科)。

思考:请结合 static 修饰属性进行更好的类设计。

代码

public class StudentTest {
    
    

	public static void main(String[] args) {
    
    
		
		Student.company = "开课吧";
		Student.subject = "java";
		Student s1 = new Student("张三", '男',18, "rap");
		Student s2 = new Student("囡囡", '女',16, "piano");		
		Student s3 = new Student("瑶", '女',28, "辅助");			
		s1.info();
		s2.info();
		s3.info();		
	}
}

class Student {
    
    
	private String name;
	private int age;
	private char sex;
	private String hobby;
	static String company;
	static String subject;
	
	Student(){
    
    
		
	}
	
	Student(String name,char sex,int age,String hooby){
    
    
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.hobby = hooby;
	}
	
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	
	public int getAge() {
    
    
		return age;
	}
	public void setAge(int age) {
    
    
		if(age > 0 || age < 200) {
    
    
			this.age = age;
		}else {
    
    
			System.out.println("您输入年龄错误!");
			this.age = 1;
		}
	}
	
	public char getSex() {
    
    
		return sex;
	}
	public void setSex(char sex) {
    
    
		this.sex = sex;
	}
	
	public String getHobby() {
    
    
		return hobby;
	}
	public void setHobby(String hobby) {
    
    
		this.hobby = hobby;
	}
	
	public void info() {
    
    
		System.out.println("姓名: " + name + "\t性别:" + sex + "\t年龄:" + age + "\t爱好:" + hobby + "\t公司:" + company + "\t课程:" + subject );
	}
}

运行结果:
在这里插入图片描述

3、通过类描述衣服, 每个衣服对象创建时需要自动生成一个序号值

问题描述

要求:每个衣服的序号是不同的, 且是依次递增 1 的

解题思路

由题意知,每创建一个对象就需要将编号+1,这里很明确指出每创建一个对象 内部就有变化,那可以采用 构造代码块来将编号+1。

代码

public class ClothesTest {
    
    

	public static void main(String[] args) {
    
    
		Clothes c1 = new Clothes("短袖", "M", "红");
		Clothes c2 = new Clothes("长袖", "L", "白");
		Clothes c3 = new Clothes("羽绒服", "M", "白");

		c1.info();
		c2.info();
		c3.info();
	}
}

class Clothes {
    
    

	private static int count = 1; // 计数
	private int ID; // 赋予编号
	private String color;
	private String size;
	private String type;
	
	Clothes(){
    
    
		
	}
	
	Clothes(String type,String size,String color){
    
    
		this.type = type;
		this.size = size;
		this.color = color;
	}
	
	/*
	 * 构造代码块 每次创建新的对象时均会调用
	 * 执行顺序:静态代码块>构造代码块>构造方法
	 */
	{
    
    
		this.ID = count++;
	}

	public String getColor() {
    
    
		return color;
	}

	public void setColor(String color) {
    
    
		this.color = color;
	}

	public String getSize() {
    
    
		return size;
	}

	public void setSize(String size) {
    
    
		this.size = size;
	}

	public String getType() {
    
    
		return type;
	}

	public void setType(String type) {
    
    
		this.type = type;
	}
	
	public void info() {
    
    
		System.out.println("编号:" + ID + "\t类型:" + type + "\t大小:" + size + "\t颜色:" + color);
	}
}

运行结果:
在这里插入图片描述

关键技术

构造代码块 :在类中的成员代码块, 我们称其为构造代码块

  • 随着对象的每次创建,均会执行一次。 且执行在构造方法之前;
  • 执行顺序:静态代码块 > 构造代码块 > 构造方法

猜你喜欢

转载自blog.csdn.net/weixin_46312449/article/details/112645668
今日推荐