- 10.20
- 学习内容
- ...
- 方法重载
- 目的
- 继承
- extends
- 继承树
- 多重继承?
- super
- 阻止继承(final)
- 向上转型
- protected
- 多态
- 覆写(override)
- 多态
- 调用被覆写方法
- 阻止覆写(final)
- 接口
- interface
- implements
- 一类多接口
- 接口继承
- 练习内容
- 1.控制台输出练习
- 2.树型结构及其遍历
- 3.树型结构,选择性打印
java 中,... 表示该参数是一个可变长参数,表示这个位置可以传入任意个该类型的参数。简而言之,就是一个数组。
方法重载在一个类中,我们可以定义多个方法。如果有一系列方法,它们的功能都是类似的,只有参数有所不同,那么,可以把这一组方法名做成同名方法。这种方法名相同,但方法参数不同,称为方法重载。
例如:
class Cat{
public void sleep(){
System.out.println("zzzZZZ");
}
public void sleep(int a){
System.out.println("zzz for " + a + " hours" );
}
}
在 sleep 方法中,不传参数和传一个整型数字作为参数,调用的方法是不一样的。
目的调用起来更简单,不必记太多方法名。
继承为什么要继承?
有时一个类与另一个类大部分字段、方法都是一样的,但又多了一些字段、方法。这时,重新声明一个类会使得代码变得重复、冗长、不美观。这时,继承就派上用场了。
让一个类从另一个类继承时,以 Student 类从 Person 类继承为例, Student 类可以获得 Person 类的所有功能,并且我们可以为 Student 类编写新增的功能。
extendsjava 中,使用 extends 关键字来实现继承。
class Student extends Person{
}
继承树
java 中,所有类都是 Object 类的子类(除了它自己)。在声明一个类时 (如 person),我们无需声明它是 Object 类的子类,编译器会自动加上 extends Object 关键字。
多重继承?java 中,只允许一个 class 继承自一个类,即,一个类有且仅有一个父类(Object 类除外)。
supersuper 表示父类。子类引用父类的字段时,可以使用 super.字段名
阻止继承(final)如果某个类有 final修饰符, 那么这个类不能被继承。
从Java 15开始,允许使用sealed修饰类,并通过permits明确写出能够从该class继承的子类名称。
例如:
public saled class SchoolPerson permits Teacher, Student{
}
在这段代码中, SchoolPerson 类只允许 Teacher、Student 两个类继承它。
向上转型把一个子类类型安全地变为父类类型的赋值,被称为向上转型(upcasting)。
如:
Person p = new Student(); //可以编译protected
继承有一个特点:子类无法访问父类的 private 字段或方法。
但是如果把 private 改成 protected ,就可以被子类访问了。
再复习一遍 public 、private 、protected 的区别。
public:公有。对所有用户开放,所有用户都可以进行直接调用。
private:私有。权限仅限于类的内部。大多数成员变量都用 private 修饰。
protected :子类可以使用,其它的不行。
多态 覆写(override)子类中如果定义了一个父类中方法签名完全相同的方法,就称为覆写。
class Cat{
public void run{
run!!
}
}
class LazyCat extends Cat{
@override //这个表示重写
public void run{
No , never!
}
}
多态
Java 的实例方法调用是基于运行时的实际类型的动态调用,而非变量的声明类型。
这个特性称为多态。
多态具有一个非常强大的功能,就是允许添加更多类型的子类实现功能扩展,却不需要修改基于父类的代码。
调用被覆写方法在子类的覆写方法中,如果要调用父类的被覆写的方法,可以通过super来调用。
例如:super.run → run!!
阻止覆写(final)将方法标记为 final ,则该方法不能被覆写。
接口 interface如果一个抽象类没有字段,所有方法都是抽象方法,那么就可以把该抽象类改为接口:interface
在java中,使用 interface 可以声明一个接口。
interface Person {
void run();
String getName();
}
implements
当一个具体的类去实现一个 interface 时,需要使用 implements 关键字。
class Student implements Person {
private String name;
public Student(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(this.name + " run");
}
@Override
public String getName() {
return this.name;
}
}
一类多接口
在 java 中,一个类只能继承自另一个类,不能从多个类继承,但是一个类可以实现多个接口。
例如:
class OrangeCat implemtes Cat , Pig{}
接口继承
一个接口可以继承自另一个接口,使用 extends 关键字。
练习内容 1.控制台输出练习(1) 打印出一个菜单。
(2) 通过输入序号就能打印出菜名和总价
(3) 已选择的序号不能再重复选择,并给出提示
(4) 输入全部序号后显示全部菜名和总价并自动退出终端
package com.xxm.task4;
import java.util.ArrayList;
import java.util.Scanner;
public class task4_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1.番茄炒鸡蛋 15元");
System.out.println("2.鱼香肉丝 20元");
System.out.println("3.小炒黄牛肉 29元");
System.out.println("4.油淋茄子 18元");
System.out.println("5.清蒸鳜鱼 32元");
int i = 1;
int totalPrice = 0;
String s = "已选:";
ArrayList list = new ArrayList(5);
while (i <= 5) {
int number = sc.nextInt();
if (list.contains(number)) {
System.out.println("这个菜已经点过了");
continue;
} else {
list.add(number);
switch (number) {
case 1:
if (i != 1) {
s = s + "、";
}
s = s + "番茄炒鸡蛋";
totalPrice = totalPrice + 15;
System.out.println(s);
System.out.println(totalPrice);
i++;
break;
case 2:
if (i != 1) {
s = s + "、";
}
s = s + "鱼香肉丝";
totalPrice = totalPrice + 20;
System.out.println(s);
System.out.println(totalPrice);
i++;
break;
case 3:
if (i != 1) {
s = s + "、";
}
s = s + "小炒黄牛肉";
totalPrice = totalPrice + 29;
System.out.println(s);
System.out.println(totalPrice);
i++;
break;
case 4:
if (i != 1) {
s = s + "、";
}
s = s + "油淋茄子";
totalPrice = totalPrice + 18;
System.out.println(s);
System.out.println(totalPrice);
i++;
break;
case 5:
if (i != 1) {
s = s + "、";
}
s = s + "清蒸鳜鱼";
totalPrice = totalPrice + 32;
System.out.println(s);
System.out.println(totalPrice);
i++;
break;
}
}
}
}
}
2.树型结构及其遍历
package com.xxm.task4;
import javax.print.DocFlavor;
import javax.print.attribute.standard.NumberOfdocuments;
import java.util.ArrayList;
import java.util.List;
public class Task4_2 {
public static class Node {
private T data = null;
private List> children = new ArrayList<>();
private Node parent = null;
//定义Node类,初始化节点数据和父节点、子节点
public Node(T data) {
this.data = data;
}
//Node 类数据初始化设置方法
public T getData() {
return data;
}
//Node 类数据的获取方法
public void setData(T data) {
this.data = data;
}
//Node 类数据的设置方法
public void setParent(Node parent) {
this.parent = parent;
}
//父节点设置方法
public Node getParent() {
return parent;
}
//获取父节点的方法
public void setChildren(List> children) {
this.children = children;
}
//设置多个子节点的方法
public List> getChildren() {
return children;
}
//获取子节点的方法
public Node addChild(Node child) {
child.setParent(this);
this.children.add(child);
return child;
}
//定义增加单个子节点的方法
public void addChildren(List> children) {
children.forEach(each -> each.setParent(this));
this.children.addAll(children);
}
//增加多个子节点的方法
}
public static void main(String[] args) {
Node node1CN = new Node("1.中国");
Node node1_1 = node1CN.addChild(new Node("1.北京"));
Node node1_1_1 = node1_1.addChild(new Node("1.朝阳区"));
Node node1_1_2 = node1_1.addChild(new Node("2.海淀区"));
Node node1_1_3 = node1_1.addChild(new Node("3.昌平区"));
Node node1_2 = node1CN.addChild(new Node("2.湖北"));
Node node1_2_1 = node1_2.addChild(new Node("1.武汉市"));
Node node1_2_2 = node1_2.addChild(new Node("2.襄樊市"));
Node node1_2_3 = node1_2.addChild(new Node("3.咸宁市"));
Node node1_3 = node1CN.addChild(new Node("3.湖南"));
Node node1_3_1 = node1_3.addChild(new Node("1.长沙市"));
Node node1_3_2 = node1_3.addChild(new Node("2.株洲市"));
Node node1_3_3 = node1_3.addChild(new Node("3.湘潭市"));
Node node2US = new Node("2.美国");
Node node2_1 = node2US.addChild(new Node("1.纽约州"));
Node node2_2 = node2US.addChild(new Node("2.得克萨斯"));
Node node2_3 = node2US.addChild(new Node("3.加利福尼亚"));
Node node2_1_1 = node2_1.addChild(new Node("1.曼哈顿"));
Node node2_1_2 = node2_1.addChild(new Node("2.得克萨斯"));
Node node2_1_3 = node2_1.addChild(new Node("3.加利福尼亚"));
Node node2_2_1 = node2_2.addChild(new Node("1.奥斯汀"));
Node node2_2_2 = node2_2.addChild(new Node("2.休斯顿"));
Node node2_2_3 = node2_2.addChild(new Node("3.达拉斯"));
Node node2_3_1 = node2_3.addChild(new Node("1.洛杉矶"));
Node node2_3_2 = node2_3.addChild(new Node("2.旧金山"));
Node node2_3_3 = node2_3.addChild(new Node("3.奥克兰"));
Node rootNode = new Node("");
rootNode.addChild(node1CN);
rootNode.addChild(node2US);
//给树型结构的各结点赋值
printTree(rootNode, " ");
//遍历打印整个树型结构
}
public static void printTree(Node node, String appender) {
System.out.println(appender + node.getData());
node.getChildren().forEach(each -> printTree(each, appender + appender));
}
}
3.树型结构,选择性打印
package com.xxm.task4;
import java.util.Scanner;
public class Task4_3 {
public static void main(String[] args) {
String s1 = "1.中国";
String s1_1 = "1.北京";
String s1_2 = "2.湖北";
String s1_3 = "3.湖南";
String s1_1_1 = "1.朝阳区";
String s1_1_2 = "1.海淀区";
String s1_1_3 = "1.昌平区";
String s1_2_1 = "1.武汉市";
String s1_2_2 = "2.襄樊市";
String s1_2_3 = "2.咸宁市";
String s1_3_1 = "1.长沙市";
String s1_3_2 = "2.株洲市";
String s1_3_3 = "3.湘潭市";
String s2 = "2.美国";
String s2_1 = "1.纽约州";
String s2_2 = "2.德克萨斯";
String s2_3 = "3.加利福尼亚";
String s2_1_1 = "1.曼哈顿";
String s2_1_2 = "2.布鲁克林";
String s2_1_3 = "3.皇后区";
String s2_2_1 = "1.奥斯汀";
String s2_2_2 = "2.休斯顿";
String s2_2_3 = "3.达拉斯";
String s2_3_1 = "1.洛杉矶";
String s2_3_2 = "2.旧金山";
String s2_3_3 = "3.奥克兰";
//定义各地区的名字
String[] country = {s1, s2};
String[][] province = {{s1_1, s1_2, s1_3}, {s2_1, s2_2, s2_3}};
String[][][] position = {
{
{s1_1_1, s1_1_2, s1_1_3},
{s1_2_1, s1_2_2, s1_2_3},
{s1_3_1, s1_3_2, s1_3_3},
},
{
{s2_1_1, s2_1_2, s2_1_3},
{s2_2_1, s2_2_2, s2_2_3},
{s2_3_1, s2_3_2, s2_3_3},
}
};
//以多维数组的方式记录数据
Scanner sc = new Scanner(System.in);
int index1;
int index2;
while (true) {
int n1 = sc.nextInt();
if (n1 != 1 && n1 != 2) {
System.out.println("输入错误,请重新输入");
continue;
} else {
index1 = n1-1;
for (String s : province[index1]
) {
System.out.println(s);
}
break;
}
}
int n2 = sc.nextInt();
while (true) {
if ((n2 != 1 && n2 != 2) && n2 != 3) {
System.out.println("输入错误,请重新输入");
continue;
} else {
index2 = n2-1;
for (String s : position[index1][index2]
) {
System.out.println(s);
}
break;
}
}
int n3 = sc.nextInt();
while (true) {
if ((n3 != 1 && n3 != 2) && n3 != 3) {
System.out.println("输入错误,请重新输入");
continue;
} else {
System.out.println(position[index1][index2][n3-1]);
}
break;
}
}
}



