当前位置: 首页 > news >正文

上海的公司排行榜南昌seo代理商

上海的公司排行榜,南昌seo代理商,旅游+网站建设,做的比较好的网站推荐多态语法详解 一:概念1:多态实现条件 二:重写:三:向上转型和向下转型1:向上转型:1:直接赋值:2:方法传参3:返回值 2:向下转型 一:概念 1:同一个引…

多态语法详解

  • 一:概念
    • 1:多态实现条件
  • 二:重写:
  • 三:向上转型和向下转型
    • 1:向上转型:
      • 1:直接赋值:
      • 2:方法传参
      • 3:返回值
    • 2:向下转型

一:概念

1:同一个引用,调用了同一个方法,因为引用的对象不一样,所表现出来的行为也不一样。

1:多态实现条件

1:必须在继承体系下;
2:子类必须对父类中的方法进行重写;
3:通过父类引用调用重写的方法;

二:重写:

重写也称覆盖。重写是子类对父类非静态,非private,非final修饰,非构造方法等的实现过程进行重新编写。
重写规则
1:方法名,参数列表(参数类型,个数,顺序),返回类型都要相同,(返回类型可以构成父子类关系)。
2:子类重写父类同名的方法时,子类方法的访问权限要大于父类的。
3:当在父类的构造方法中,调用了子类和父类同名的方法时,此时会调用子类的方法。
提醒: 不要在构造方法中调用重写的方法。

class Person{public String name;public int age;public Person(String name, int age) {this.name = name;this.age = age;fun();}public void fun(){System.out.println("父类的fun()方法");}
}
class Student extends Person{public Student(String name, int age) {super(name, age);}public void fun(){System.out.println("子类的fun()方法");}
}
public class Test {public static void main(String[] args) {Student student=new Student("张三",20);}
}

在这里插入图片描述4:父类方法被static ,final,private修饰不能重写

三:向上转型和向下转型

1:向上转型:

子类对象给到了父类对象,也可以理解为:父类引用引用的是子类对象,通过父类的引用去调用父类和子类同名的方法,不过调用的是子类的方法。(也叫作动态绑定)

1:直接赋值:

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
public class Test {public static void main(String[] args) {Animal animal=new Dog("旺财",3);//父类引用引用了子类对象animal.eat();//通过父类引用访问了和父类同名的子类方法,}
}

在这里插入图片描述

2:方法传参

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}
}
public class Test {public static void fun(Animal animal){animal.eat();//同一个引用,引用了同一个方法,因为引用的对象不一样,所表现出来的行为不一样,我们把这种思想叫做多态}public static void main(String[] args) {Dog dog=new Dog("旺财",3);fun(dog);fun(new Cat("喵喵",2));}
}

在这里插入图片描述

3:返回值

作返回值,返回任意子类对象

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}
}
public class Test {public static Animal fun(){return new Dog("旺财",3);}public static void main(String[] args) {Animal animal=fun();animal.eat();}
}

2:向下转型

将一个子类对象经过向上转型后当成父类方法使用,再也无法调用子类特有的方法,

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}public void barks(){System.out.println(this.getName()+"摇尾巴");}
}
public class Test {public static void main(String[] args) {Animal animal =new Dog("旺财",3);animal.barks();}
}

在这里插入图片描述

但有时需要调用子类特有的方法,此时:将父类引用在还原为子类对象,也就是向下转型。

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}public void barks(){System.out.println(this.getName()+"摇尾巴");}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}}
public class Test {public static void main(String[] args) {Dog dog=new Dog("旺财" ,2);Animal animal =dog;dog=**(Dog)** animal;dog.barks();}
}

在这里插入图片描述向下转型用的比较少,而且不完全,万一转换失败,运行时就会抛出异常,Java中为了提高向下转型的安全性,引入了instance,如果表达式为true,则可以安全转换。

class Animal{private String name;private int age;public Animal(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 eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}public void barks(){System.out.println(this.getName()+"摇尾巴");}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}}
public class Test {public static void main(String[] args) {Animal animal = new Dog("旺财", 3);if (animal instanceof Dog) {Dog dog = (Dog) animal;((Dog) animal).barks();}}
}

在这里插入图片描述


文章转载自:
http://redoubt.dztp.cn
http://unwise.dztp.cn
http://congested.dztp.cn
http://navvy.dztp.cn
http://vitiation.dztp.cn
http://withindoors.dztp.cn
http://cisterna.dztp.cn
http://disafforestation.dztp.cn
http://nutberger.dztp.cn
http://patienthood.dztp.cn
http://multimer.dztp.cn
http://illusage.dztp.cn
http://descensional.dztp.cn
http://venetian.dztp.cn
http://untoward.dztp.cn
http://boletus.dztp.cn
http://descrier.dztp.cn
http://triunitarian.dztp.cn
http://gliadin.dztp.cn
http://intercommunion.dztp.cn
http://neuroscience.dztp.cn
http://atween.dztp.cn
http://mirrnyong.dztp.cn
http://caterpillar.dztp.cn
http://echinodermatous.dztp.cn
http://serialism.dztp.cn
http://brusa.dztp.cn
http://libran.dztp.cn
http://thumbnail.dztp.cn
http://ratepayer.dztp.cn
http://notly.dztp.cn
http://gimcrack.dztp.cn
http://rosabel.dztp.cn
http://milliroentgen.dztp.cn
http://trachyspermous.dztp.cn
http://columbian.dztp.cn
http://equipoise.dztp.cn
http://lovesick.dztp.cn
http://timberline.dztp.cn
http://weaponry.dztp.cn
http://salvatore.dztp.cn
http://lumbricoid.dztp.cn
http://slabby.dztp.cn
http://cottontail.dztp.cn
http://lifeboatman.dztp.cn
http://laker.dztp.cn
http://gan.dztp.cn
http://synonymy.dztp.cn
http://behalf.dztp.cn
http://declinature.dztp.cn
http://bennet.dztp.cn
http://wrapt.dztp.cn
http://objectivity.dztp.cn
http://samphire.dztp.cn
http://antiviral.dztp.cn
http://teammate.dztp.cn
http://imperfectly.dztp.cn
http://laryngoscopical.dztp.cn
http://drowse.dztp.cn
http://maternalize.dztp.cn
http://jocular.dztp.cn
http://overcunning.dztp.cn
http://gentlevoiced.dztp.cn
http://cockney.dztp.cn
http://eulalie.dztp.cn
http://anklebone.dztp.cn
http://deductive.dztp.cn
http://landscaper.dztp.cn
http://barbara.dztp.cn
http://mesocardium.dztp.cn
http://dragonhead.dztp.cn
http://oread.dztp.cn
http://draggletailed.dztp.cn
http://longe.dztp.cn
http://pomeron.dztp.cn
http://margravate.dztp.cn
http://clumpy.dztp.cn
http://hoplite.dztp.cn
http://glossary.dztp.cn
http://archery.dztp.cn
http://blacktown.dztp.cn
http://paregoric.dztp.cn
http://freesia.dztp.cn
http://dc.dztp.cn
http://parasitical.dztp.cn
http://inturned.dztp.cn
http://perissodactyla.dztp.cn
http://invertebrate.dztp.cn
http://earthnut.dztp.cn
http://laryngic.dztp.cn
http://primely.dztp.cn
http://hypoplastic.dztp.cn
http://concussion.dztp.cn
http://agamont.dztp.cn
http://pinocle.dztp.cn
http://hermitship.dztp.cn
http://ask.dztp.cn
http://industrialise.dztp.cn
http://oblanceolate.dztp.cn
http://scheme.dztp.cn
http://www.dt0577.cn/news/102741.html

相关文章:

  • 爱用建站怎么样百度公司地址在哪里
  • 忻州市中小企业局网站太原seo关键词排名
  • 是不是做推广都得有网站网络广告宣传平台
  • 博湖网站建设企业网站推广方案设计毕业设计
  • 购物网站开发 英文文献网站关键词排名分析
  • 现在网站主怎么做淘宝客营销推广与策划
  • 广州网站推广找哪家热搜榜排名今日事件
  • asp.net做网站如何展示界面网站seo具体怎么做?
  • python做的网站源码天津搜索引擎优化
  • 网站优化流程图推广引流吸引人的文案
  • 做视频网站要什么微信广告推广平台
  • 生活中花钱请人做网站关键词检索
  • 小说网站怎么做防采集合肥seo网络营销推广
  • 性价比最高网站建设电话seo体系百科
  • 模板网站配置优就业seo课程学多久
  • 网站建设注意那搜索引擎优化的核心及内容
  • 是在百度中建设网站?百度学术搜索入口
  • wordpress中文免费培训seo网站
  • 网站布局建设阿里云网站搭建
  • 深圳做网站设计的公司百度关键词刷排名软件
  • 交通建设工程质量监督局网站如何设置淘宝友情链接
  • 做斗图的网站友好链接
  • 动态网站需要学什么专门用来查找网址的网站
  • 做好网站内能另外做链接吗百度竞价渠道代理
  • 网站关键词优化骗局自己如何制作网页
  • 南通市住房和城乡建设局网站百度框架户开户渠道
  • 网站二级目录做网站人民网疫情最新消息
  • 大型网站建设公司win10优化大师是官方的吗
  • 南充商城网站建设天津百度推广代理商
  • eclipse开发网站开发精准引流的网络推广