1-5
常用cmd命令
dir可以查看隐藏的文件,
exit 退出
6-20
20-30
30-40
37 三元表达式
switch新特性
统计
ctrl+alt+M 自动抽取方法
选中一个要改的,shift+F6 同时改方法中的全部
78
88
99
倒序遍历
106
退出while循环
121 static
129 多态
多态案例
package com.itheima.duotai;
public class Animal {
private int age;
private String color;
public Animal() {
}
public Animal(int age, String color) {
this.age = age;
this.color = color;
}
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 eat(String something){
System.out.println("动物在吃"+something);
}
}
package com.itheima.duotai;
public class Dog extends Animal{
public Dog() {
}
public Dog(int age, String color) {
super(age, color);
}
@Override
public void eat(String something) {
// super.eat(something);
System.out.println(getAge()+"岁的"+getColor()+"狗在吃"+something);
}
public void lookHome(){
System.out.println("狗在看家");
}
}
package com.itheima.duotai;
public class Cat extends Animal{
public Cat() {
}
public Cat(int age, String color) {
super(age, color);
}
@Override
public void eat(String something) {
System.out.println(getAge()+"岁的"+getColor()+"猫在吃"+something);
}
public void catchMouse(){
System.out.println("猫在抓老鼠");
}
}
package com.itheima.duotai;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 void keepPet(Dog dog,String something){
// System.out.println("年龄"+age+"岁的"+name+"养了"+dog.getAge()+"岁的"+dog.getColor()+"的狗");
// dog.eat(something);
// }
// public void keepPet(Cat cat,String something){
// System.out.println("年龄"+age+"岁的"+name+"养了"+cat.getAge()+"岁的"+cat.getColor()+"的猫");
// cat.eat(something);
// }
// public void keepPet(Animal a,String something){
// if (a instanceof Dog dog){
// System.out.println("年龄"+age+"岁的"+name+"养了"+a.getAge()+"岁的"+a.getColor()+"的动物");
// dog.eat(something);
// dog.lookHome();
// }else if (a instanceof Cat cat){
// System.out.println("年龄"+age+"岁的"+name+"养了"+cat.getAge()+"岁的"+cat.getColor()+"的动物");
// cat.eat(something);
// cat.catchMouse();
// }else {
// System.out.println("没有这种动物");
// }
// }
public void keepPet(Animal a, String something) {
if (a instanceof Dog) {
Dog dog = (Dog) a;
System.out.println("年龄" + age + "岁的" + name + "养了" + a.getAge() + "岁的" + a.getColor() + "的动物");
dog.eat(something);
dog.lookHome();
} else if (a instanceof Cat ) {
Cat cat = (Cat) a;
System.out.println("年龄" + age + "岁的" + name + "养了" + cat.getAge() + "岁的" + cat.getColor() + "的动物");
cat.eat(something);
cat.catchMouse();
} else {
System.out.println("没有这种动物");
}
}
}
package com.itheima.duotai;
public class Test {
public static void main(String[] args) {
// Person p1 = new Person("老王",30);
// Dog dog = new Dog(3,"黑色");
// p1.keepPet(dog,"骨头");
//
// Person p2 = new Person("老李",20);
// Cat cat = new Cat(2, "黄色");
// p2.keepPet(cat,"鱼");
Person p1 = new Person("老王", 30);
Dog dog = new Dog(3, "黑色");
p1.keepPet(dog, "骨头");
Cat cat = new Cat(2, "黄色");
p1.keepPet(cat,"鱼");
}
}
package com.itheima.opp;
public abstract class Animal {
private String name;
private String age;
public Animal() {
}
public Animal(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public abstract void eat();
}
package com.itheima.opp;
public interface Swim {
public abstract void swim();
}
package com.itheima.opp;
public class Rabbit extends Animal{
public Rabbit() {
}
public Rabbit(String name, String age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("兔子吃");
}
}
package com.itheima.opp;
public class Frog extends Animal implements Swim{
public Frog() {
}
public Frog(String name, String age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("青蛙在吃虫子");
}
@Override
public void swim() {
System.out.println("青蛙在游泳");
}
}
package com.itheima.opp;
public class Dog extends Animal implements Swim{
public Dog() {
}
public Dog(String name, String age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗在吃骨头");
}
@Override
public void swim() {
System.out.println("狗在游泳");
}
}
package com.itheima.opp;
public class Test {
public static void main(String[] args) {
Frog frog = new Frog("forg","1");
frog.eat();
frog.swim();
}
}