5.15Java学习笔记

一. 代码复用的好处

1. 简化代码

2. 一处修改,处处修改

二. 什么是继承?

1. Java世界中同样也有继承关系,和现实世界中的继承类似。

2. 继承是从已有的类中派生出新类,新的类能吸收已有类的数据属性行为,并能扩展新的能力。

3. 继承是面向对象的三大特征之一。(多态、封装、继承)

三. 继承的语法格式

修饰符 class 子类 extends 父类{ }

                                 ↑(继承的关键字)

四. 继承的优点

1. 提高代码的复用性

2. 提高可维护性

3. 简化了逻辑

五. 继承的特点

1. 继承关系具有传递性(例如类A有父类B,类B拥有父类C,则C也可以称为是A的父类(不是直接父类))

2. 单继承

六. 方法的重写

1. 什么是方法的重写

当子类中出现与父类一模一样的方法时,子类对象调用该方法,会运行子类方法的内容,如同父类的方法被覆盖一样。这种情况是方法的另一种特性——重写。

覆盖的应用

当子类需要父类的功能,而功能主体子类有自己持有内容时,可以复写父类中的方法,这样,既沿袭了父类的功能,又定义了子类持有的功能。

2.方法重载和方法重写区别(面试题)

            方法重写Override:

                继承中子类中出现与父类方法声明完全相同的情况;

                (返回值、方法名、参数都一模一样)

            方法重载Overload:

                   同一类中,方法名相同,参数列表不同; 

3. 如何进行方法的重写

方法重写的规则

① 方法名相同

② 形参列表相同

③ 子类的方法的返回值应该比父类的返回值类型更小或相等

④ 子类方法抛出的异常类应该比父类抛出的异常类更小或相等

⑤ 子类方法的访问权限应该比父类方法访问权限更大或相等

                         

package com.lenovo.demo;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 图书管理系统
 *
 * @author lenovo
 *
 */
public class Library {
	private static ArrayList<Book> booklist = new ArrayList<>();
	private static int n;

	public static void main(String[] args) {

		do {
			listMenu();

		} while (n != 0);
		System.out.println("程序退出");

	}

	/**
	 * 列出所有图书
	 */

	private static void listAllBook() {
		for (Book book : booklist) {
			System.out.println("书名:" + book.getName() + ",出版社:" + book.getPublish() + ",作者:" + book.getAuthor() + ",页码:"
					+ book.getPages() + ",价格:" + book.getPrice());
		}
	}

	/**
	 * 添加图书
	 */
	private static void addBook() {
		System.out.println("请选择您要添加的图书类型:1.Book;2.Ebook;3.Magazine");
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		switch (n) {
		case 1:
			System.out.println("请输入书名:");
			sc.nextLine();
			String name = sc.nextLine();
			System.out.println("请输入出版社:");
			String publish = sc.nextLine();
			System.out.println("请输入作者:");
			String author = sc.nextLine();
			System.out.println("请输入页数:");
			int pages = sc.nextInt();
			System.out.println("请输入价格:");
			double price = sc.nextDouble();

			// 创建一个book对象
			Book book = new Book(name, price, pages, publish, author);
			// 添加到ArrayList中
			booklist.add(book);

			break;
		case 2:
			break;
		case 3:
			break;

		default:
			break;
		}

	}

	private static void listMenu() {
		System.out.println("请输入菜单序号:");
		System.out.println("查看菜单:1");
		System.out.println("添加图书:2");
		System.out.println("删除图书:3");
		System.out.println("退出程序:0");
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		System.out.println("您选择的功能的序号是:" + n);
		switch (n) {
		case 1:
			listAllBook();
			break;
		case 2:
			addBook();
			break;
		case 3:
			break;

		default:
			break;

		}

	}
}


package com.lenovo.demo;
/**
 * 纸质书
 * @author lenovo
 *
 */
public class Book {
	private String name;
	private double price;
	private int pages;
	private String publish;
	private String author;

	public Book() {

	}

	public Book(String name, double price, int pages, String publish, String author) {
		super();
		this.name = name;
		this.price = price;
		this.pages = pages;
		this.publish = publish;
		this.author = author;
	}

	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 int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public String getPublish() {
		return publish;
	}

	public void setPublish(String publish) {
		this.publish = publish;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

}


package com.lenovo.demo;
/**
 * 电子书
 * @author lenovo
 *
 */
public class EBook {
	private String name;
	private double price;
	private int pages;
	private int downliadTimes;
	private String author;
	
	public EBook() {}

	public EBook(String name, double price, int pages, int downliadTimes, String author) {
		super();
		this.name = name;
		this.price = price;
		this.pages = pages;
		this.downliadTimes = downliadTimes;
		this.author = author;
	}

	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 int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getDownliadTimes() {
		return downliadTimes;
	}

	public void setDownliadTimes(int downliadTimes) {
		this.downliadTimes = downliadTimes;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
	
}
	

package com.lenovo.demo;

/**
 * 杂志
 *
 * @author lenovo
 *
 */
public class Magazine {
	private String name;
	private double price;
	private int pages;
	private String issue;
	

public Magazine() {
	
}


public Magazine(String name, double price, int pages, String issue) {
	super();
	this.name = name;
	this.price = price;
	this.pages = pages;
	this.issue = issue;
}


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 int getPages() {
	return pages;
}


public void setPages(int pages) {
	this.pages = pages;
}


public String getIssue() {
	return issue;
}


public void setIssue(String issue) {
	this.issue = issue;
}


	

}

输出结果: 
父类的方法 
子类重写的方法

去掉super.setName();输出结果: 
子类重写的方法



猜你喜欢

转载自blog.csdn.net/qq_42158426/article/details/80323476