快捷键:
'ctrl' + '+',放大代码
‘ctrl' + '-',缩小代码
1.使用多态的优点
把要创建的多个子类缩减为一个父类接着传入参数,用参数调用子类的方法,
输出时直接调用父类的方法,这时传参传创建的对象
class Animal{
void eat() {
System.out.println("动物吃东西");
}
}
class cat extends Animal{
void eat() {
System.out.println("猫吃鱼");
}
}
class dog extends Animal{
void eat() {
System.out.println("狗吃骨头");
}
}
class Person{
void feedAnimal(cat c) {
c.eat();
}
void feedAnimal(dog d) {
d.eat();
}
··········
若要喂一百只动物,则需要写一百个方法。
}
public class 多态 {
public static void main(String[] args) {
cat c = new cat();
dog d = new dog();
Person per = new Person();
per.feedAnimal(c);
per.feedAnimal(d);
}
}
【
猫吃鱼
狗吃骨头
】
class Animal{
void eat() {
System.out.println("动物吃东西");
}
}
class cat extends Animal{
void eat() {
System.out.println("猫吃鱼");
}
}
class dog extends Animal{
void eat() {
System.out.println("狗吃骨头");
}
}
class Person{
void feedAnimal(Animal anim) {
anim.eat();
}
使用多态后,只需要写一个方法
}
public class 多态 {
public static void main(String[] args) {
Animal c = new cat();
dog d = new dog();
Person per = new Person();
per.feedAnimal(c);
per.feedAnimal(d);
}
}
【
猫吃鱼
狗吃骨头
】
2.多态方法的调用
在父类的方法前加static后,会默认调用父类的方法(原因:static修饰的内容会随着
字节码的加载而加载,不会去考虑后来创建的对象)
3.类的强制类型转换
把父类对象所属类型强制转换成子类类型
4.instanceof:判断一个对象是否属于指定的类
class Animal{
void eat() {
System.out.println("动物吃东西");
}
}
class cat extends Animal{
void eat() {
System.out.println("猫吃鱼");
}
void dowork() {
System.out.println("逮老鼠");
}
}
class dog extends Animal{
void eat() {
System.out.println("狗吃骨头");
}
void dowork() {
System.out.println("汪汪汪");
}
}
class Person{
void feedAnimal(Animal anim) {
anim.eat();
}
void dowork(Animal anim) {
if(anim instanceof dog) { //判断这个参数是否属于狗类
dog d=(dog)anim; //类的强制类型转换(父类中并没有dowork这个方法)
d.dowork();
}else if(anim instanceof cat) {
cat c=(cat)anim;
c.dowork();
}
}
}
public class 多态 {
public static void main(String[] args) {
Animal c = new cat();
dog d = new dog();
Person per = new Person();
per.feedAnimal(d);
per.dowork(d);
per.feedAnimal(c);
per.dowork(c);
}
eg:
class sub{
String name = "ss";
void a(){
System.out.println("sub-1");
}
}
class sup extends sub{
String name = "qq";
void a(){
System.out.println("sup-2");
}
}
public class 多态2 {
public static void main(String[] args) {
sub u = new sup(); //u使用name这个字段的时候,会选择调用父类sub的
u.a();
System.out.println(u.name);
}
}
【sup-2
ss】
2.封装小练习(一个问题两种方法)
- 创建学生类
创建成员变量:name,age
构造类:无参,两个参数
使用getset方法
show展示和toString展示成员变量
法一:构造器中传参
class Stu{
private int age;
private String name;
Stu(String name,int age){
this.name = name;
this.age = age;
}
void show(){
System.out.print(name+" ");
System.out.print(age);
System.out.println();
}
}
public class 封装练习 {
public static void main(String[] args) {
Stu student2 = new Stu("lx",18);
student2.show();
}
}
【lx 18】
法二:采用getset方法使字段能让外界使用
class Stu{
private int age;
private String name;
//默认存在Stu(){ }这个构造器
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
void show(){
System.out.print(name+" ");
System.out.print(age);
System.out.println();
}
}
public class 封装练习 {
public static void main(String[] args) {
Stu student1 = new Stu();
student1.setName("hh");
student1.setAge(17);
student1.show();
}
}
【hh 17】
- 封装长方形的周长和面积
法一:构造器传参
class Rect{
private int lon;
private int height;
Rect(int lon,int height){
this.lon = lon;
this.height = height;
}
void area() {
System.out.println(lon*height);
}
void c() {
System.out.println((lon+height)*2);
}
}
public class 封装练习2 {
public static void main(String[] args) {
Rect r = new Rect(4,5);
r.area();
r.c();
}
}
法二:采用getset方法使字段能让外界使用
class Rect{
private int lon;
private int height;
public int getLon() {
return lon;
}
public void setLon(int lon) {
this.lon = lon;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
void area() {
System.out.println(lon*height);
}
void c() {
System.out.println((lon+height)*2);
}
}
public class 封装练习2 {
public static void main(String[] args) {
Rect r = new Rect();
r.setLon(4);
r.setHeight(5);
r.area();
r.c();
}
}
改进后:保证了数据的合理性。(将getset方法结合构造器传参使用)
使用private将数据保护起来,内部提供getset方法,检验输入得数据是否合理
class Rect{
private int lon;
private int height;
Rect(int lon,int height){
this.setLon(lon);
this.setHeight(height);
}
public int getLon() {
return lon;
}
public void setLon(int lon) {
if(lon <= 0) {
System.out.println("输入有误");
return;
}
this.lon = lon;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
if(height <= 0) {
System.out.println("输入有误");
return;
}
this.height = height;
}
void area() {
System.out.println(lon*height);
}
void c() {
System.out.println((lon+height)*2);
}
}
public class 封装练习2 {
public static void main(String[] args) {
Rect r = new Rect(4,5);
r.area();
r.c();
}
}