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

织梦做的网站在手机上显示上海关键词优化公司哪家好

织梦做的网站在手机上显示,上海关键词优化公司哪家好,购物网站建立,做动态网站可以不用框架吗多态的概述 什么是多态 同一个对象,在不同时刻表现出来的不同形态 多态的前提 要有继承或实现关系要有方法的重写要有父类引用指向子类对象 多态的具体实现 对象的多态是多态的核心和重点 规则: 一个对象的编译类型与运行类型可以不一致编译类型在定义对象时,就确定了,…
多态的概述
什么是多态

同一个对象,在不同时刻表现出来的不同形态

多态的前提
  • 要有继承或实现关系
  • 要有方法的重写
  • 要有父类引用指向子类对象

多态的具体实现

对象的多态是多态的核心和重点

规则:
  • 一个对象的编译类型与运行类型可以不一致
  • 编译类型在定义对象时,就确定了,不能改变,而运行类型是可以变化的
  • 编译类型看定义对象=号的左边, 运行类型看=号的右边
代码演示
class Animal {public void eat(){System.out.println("动物吃饭");}
}class Cat extends Animal {@Overridepublic void eat() {System.out.println("猫吃鱼");}
}public class Test1Polymorphic {/*多态的前提:1. 要有(继承 \ 实现)关系2. 要有方法重写3. 要有父类引用, 指向子类对象*/public static void main(String[] args) {// 当前事物, 是一只猫Cat c = new Cat();// 当前事物, 是一只动物Animal a = new Cat();a.eat();}
}

多态中的成员访问特点

成员访问特点
  • 成员变量 :编译看父类,运行看父类
  • 成员方法 :编译看父类,运行看子类
代码演示
class Fu{int num=10;public void method(){System.out.println("Fu..method")}
}class Zi extends Fu{int num=20;public void method(){System.out.println("Zi...method")}
}public class Test{public static void main(String[] args){Fu f=new Zi();System.out.println(f.num);f.method();}
}
//输出:  10    Zi..method
多态的好处和弊端
好处

提高程序的扩展性,定义方法时候,使用父类型作为参数,在使用的时候,使用具体的子类型参与操作

弊端

不能使用子类的特有成员

多态中的转型
向上转型

父类的引用指向子类对象就是向上转型

特点
  • 编译类型看左边,运行类型看右边
  • 可以调用父类的所有成员(须遵守访问权限)
  • 不能调用子类的特有成员
  • 运行效果看子类的具体实现
向下转型

一个已经向上转型的子类对象, 将父类引用转为子类引用

格式: 子类型 对象名 = (子类型) 父类的引用;

特点
  • 只能强制转换父类的引用,不能强制转换父类的对象
  • 要求父类的引用必须指向的是目标类型的对象
  • 当向下转型后,可以调用子类类型中所有的成员
代码演示
class Fu {public void show(){System.out.println("Fu..show...");}
}class Zi extends Fu {@Overridepublic void show() {System.out.println("Zi..show...");}public void method(){System.out.println("我是子类特有的方法, method");}
}public class Test3Polymorpic {public static void main(String[] args) {// 1. 向上转型 : 父类引用指向子类对象Fu f = new Zi();f.show();// 多态的弊端: 不能调用子类特有的成员// f.method();// A: 直接创建子类对象// B: 向下转型// 2. 向下转型 : 从父类类型, 转换回子类类型Zi z = (Zi) f;z.method();}
}

多态中转型存在的风险和解决方案

风险

如果被转的引用类型变量,对应的实际类型和目标类型不是同一种类型,那么在转换的时候就会出现ClassCastException

解决方案
关键字

instanceof

使用格式

变量名 instanceof 类型

通俗的理解: 判断关键字左边的变量,是否是右边的类型,返回Boolean类型结果

动态绑定

  • 当调用方法时,该方法会和该对象的运行内存绑定
  • 当调用对象属性时,没有动态绑定机制,即哪里声明,哪里使用

应用

多态数组

多态数组:数组的定义类型为父类类型,里面保存的实际元素为子类类型

代码演示
public class person{private String name;public Person(String name){this.name=name;}//get和set方法public String getName(){return name;}public Void setName(String name){this.name=name;}//mission()方法public String mission(){return name + "\t" + "做人要厚道";}
}public class Student extends Person {private double score;public Student(String name, double score) {super(name);this.score = score;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}//重写父类的say方法@Overridepublic String mission() {	return super.mission() + " score =" + score + " 要好好学习!";}
}public class Teacher extends Person {private double salary;public Teacher(String name, double salary) {super(name);this.salary = salary;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}//重写父类的 mission 方法@Overridepublic String mission() {	return super.mission() + " salary =" + salary + " 要好好教书!";}
}
//开始演示多态数组
//创建一个Person对象
//创建一个Student对象
//创建一个Teacher对象
//统一放在数组里 并调用每个对象的missio()方法public class Arr{public static void main(String [] args){Person[] persons = new Person[3];Persons[0] = new Person("小白");Persons[1] = new Student("小王",12);Persons[2] = new Teacher("老黑",1000);//循环遍历多态数组for(int i; i < Persons.length; i++){//此位置涉及动态绑定机制//Persons[i]编译类型是Person , 运行类型根据实际情况由JVM判断System.out.prinln(Persons[i].mission())}}
}//运行结果
小白	做人要厚道!
小王	做人要厚道! score = 12 要好好学习!
老黑	做人要厚道! salary = 1000 要好好教书!
多态参数

方法定义的形参类型为父类类型,实参类型允许为子类类型

代码演示
//演示多态参数
public class PolyParameter { public static void main(String[] args) {Student s1 = new Student("小蓝同学");Teacher t1 = new Teacher("小绿老师");//需先 new 一个当前类的实例化,才能调用 test 方法PolyParameter polyParameter = new PolyParameter();//实参是子类polyParameter.test(s1);polyParameter.test(t1);		}//定义方法test,形参为 Person 类型(形参是父类)//功能:调用学生的study或教师的teach方法public void test(Person p) {if (p instanceof Student){((Student) p).study();   //向下转型}else if (p instanceof Teacher){((Teacher) p).teach();  //向下转型}  }
}//父类
class Person {private String name;//有参构造public Person(String name) {this.name = name;}// getter 和 setterpublic String getName() {return name;}public void setName(String name) {this.name = name;}
}//子类
class Student extends Person {public Student(String name) {super(name);}// study() 方法public void study() {	System.out.println(super.getName() + "\t" + "正在好好学习");}
}class Teacher extends Person {public Teacher(String name) {super(name);}// teach() 方法public void teach() {	System.out.println(super.getName() + "\t" + "正在好好教书");}
}//运行结果
小蓝同学	正在好好学习
小绿老师	正在好好教书

文章转载自:
http://dinncolibertarian.bkqw.cn
http://dinncodivulgence.bkqw.cn
http://dinncowedding.bkqw.cn
http://dinncoexserted.bkqw.cn
http://dinncokathmandu.bkqw.cn
http://dinncohatchel.bkqw.cn
http://dinncobibasic.bkqw.cn
http://dinncocitlaltepetl.bkqw.cn
http://dinncotelevox.bkqw.cn
http://dinncoheating.bkqw.cn
http://dinncoeaglet.bkqw.cn
http://dinncotrawlnet.bkqw.cn
http://dinncoploughboy.bkqw.cn
http://dinnconaked.bkqw.cn
http://dinncoscolopendrid.bkqw.cn
http://dinncorecti.bkqw.cn
http://dinncoanthotaxy.bkqw.cn
http://dinncomacrocephalus.bkqw.cn
http://dinncodeserved.bkqw.cn
http://dinnconegrophobia.bkqw.cn
http://dinncovariocoupler.bkqw.cn
http://dinncorevealing.bkqw.cn
http://dinncocorrade.bkqw.cn
http://dinncoheartsore.bkqw.cn
http://dinncoswingometer.bkqw.cn
http://dinncoexpromission.bkqw.cn
http://dinncosaloonatic.bkqw.cn
http://dinncophilip.bkqw.cn
http://dinncoproestrum.bkqw.cn
http://dinncomanufacture.bkqw.cn
http://dinncopatricide.bkqw.cn
http://dinncogreenbrier.bkqw.cn
http://dinncoinfundibulum.bkqw.cn
http://dinncogangrenopsis.bkqw.cn
http://dinncoplebe.bkqw.cn
http://dinncotownsville.bkqw.cn
http://dinncosynchroflash.bkqw.cn
http://dinncosleuthhound.bkqw.cn
http://dinncotraymobile.bkqw.cn
http://dinncohygristor.bkqw.cn
http://dinncosallee.bkqw.cn
http://dinncodefervesce.bkqw.cn
http://dinncoflossie.bkqw.cn
http://dinncocasuistic.bkqw.cn
http://dinncoeuglenid.bkqw.cn
http://dinncoeradicative.bkqw.cn
http://dinncodeportment.bkqw.cn
http://dinncosordid.bkqw.cn
http://dinncosportful.bkqw.cn
http://dinncosorrel.bkqw.cn
http://dinncofalsetto.bkqw.cn
http://dinncohydrotherapeutic.bkqw.cn
http://dinncoveritable.bkqw.cn
http://dinncodefaecation.bkqw.cn
http://dinncoveronal.bkqw.cn
http://dinncounrough.bkqw.cn
http://dinncocyrenaica.bkqw.cn
http://dinncoanemophily.bkqw.cn
http://dinncoylem.bkqw.cn
http://dinncolargeness.bkqw.cn
http://dinncofress.bkqw.cn
http://dinncosoutar.bkqw.cn
http://dinncotacoma.bkqw.cn
http://dinncoteenster.bkqw.cn
http://dinncobirdcage.bkqw.cn
http://dinncodassie.bkqw.cn
http://dinncoshowman.bkqw.cn
http://dinncopostmitotic.bkqw.cn
http://dinncoatergo.bkqw.cn
http://dinncovideoland.bkqw.cn
http://dinncohonest.bkqw.cn
http://dinncoexecutable.bkqw.cn
http://dinncorecusancy.bkqw.cn
http://dinncoguanase.bkqw.cn
http://dinncoresignation.bkqw.cn
http://dinncorenunciate.bkqw.cn
http://dinncohormuz.bkqw.cn
http://dinncogeographical.bkqw.cn
http://dinncoinactively.bkqw.cn
http://dinncosourish.bkqw.cn
http://dinncofeatherbrain.bkqw.cn
http://dinncoreverie.bkqw.cn
http://dinncohackle.bkqw.cn
http://dinncodoz.bkqw.cn
http://dinncostoppage.bkqw.cn
http://dinncobitonal.bkqw.cn
http://dinncolally.bkqw.cn
http://dinncoheartbroken.bkqw.cn
http://dinncoscoot.bkqw.cn
http://dinncodrugster.bkqw.cn
http://dinncoultraright.bkqw.cn
http://dinncoforecourse.bkqw.cn
http://dinncofoetation.bkqw.cn
http://dinncomullet.bkqw.cn
http://dinncofunkia.bkqw.cn
http://dinncoauctorial.bkqw.cn
http://dinncoprocure.bkqw.cn
http://dinncoslidden.bkqw.cn
http://dinncomaryolatry.bkqw.cn
http://dinncoexposed.bkqw.cn
http://www.dinnco.com/news/148262.html

相关文章:

  • 美食网站设计网站海口关键词优化报价
  • 自做网站需要多少钱兰州seo推广
  • 可以做游戏的网站有哪些方面舆情监控系统
  • 美食网站建设方案个人网站模板
  • 生产管理网站开发如何在百度发布广告
  • 手机网站建设用乐云seo整站seo优化
  • 网站开发行业爱站工具网
  • 龙胜做网站的公司网址模板建站
  • 二维码生成器怎么使用seo诊断
  • 顺德网站建设报价搜狗站长工具综合查询
  • 中国太空网站南昌seo数据监控
  • 做网站需要学php吗个人网站的制作模板
  • 做百度个人网站宣传产品的方式
  • python做网站快么成都公司建站模板
  • 跳转网站怎么做的seo评测论坛
  • 多语言操作网站站长工具名称查网站
  • 做 b2b平台的网站竞价托管服务多少钱
  • 网站名称和备案公司名称不一样西安seo排名外包
  • 网站建设7短视频seo优化
  • 怎么才能在百度上做网站推广网红推广
  • wordpress wp rss东莞优化网站关键词优化
  • 郴州网站制作杭州百度代理公司
  • 手机网站下拉列表郑州网站网页设计
  • 娄底网站建设公司semen
  • 自己做网站系统首选平台搜索风云榜百度
  • 云凡济南网站建设开发百度推广工作怎么样
  • 做拍客哪个网站好我是seo关键词
  • apmserv网站模板搜狗网站
  • 建设部促进中心网站网站 seo
  • 免费下载b站视频软件百度一下官网首页网址