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

网站留言板模版电脑编程培训

网站留言板模版,电脑编程培训,施工企业的期间费用包括哪些,微信小程序与公众号的区别目录 一#xff0c;方法的重写 二#xff0c;重写方法的注意事项 三#xff0c;方法重写的使用场景 四#xff0c;super和this 1.继承中构造方法的特点 2.super和this的具体使用 super的具体使用 this的具体使用 一#xff0c;方法的重写 1.概述:子类中有一个和父类…目录 一方法的重写 二重写方法的注意事项 三方法重写的使用场景 四super和this 1.继承中构造方法的特点 2.super和this的具体使用 super的具体使用 this的具体使用 一方法的重写 1.概述:子类中有一个和父类方法名以及参数列表相同的方法 2.前提:继承 3.访问:看new的是谁,先调用谁中的,如果new的是子类,调用调用子类重写的方法,子类没有,找父类 4.检测是否为重写方法:在该方法上写   Override   依旧创建三个类 public class Fu {public void methodFu(){System.out.println(我是父类中的methodFu方法);}public void method(){System.out.println(我是父类中的method方法);} } public class Zi extends Fu{public void methodZi(){System.out.println(我是子类中的methodZi方法);}Overridepublic void method(){System.out.println(我是子类中的method方法);} } 如果是重写Override不会报错如果方法名和参数列表有一个不同就会报错 测试类 import com.atthruster.c_extends.Zi;public class test {public static void main(String[] args) {Fu fu new Fu();fu.methodFu();//自己的methodFu方法fu.method();//new的是父类对象,那么调用的就是父类中的methodSystem.out.println();Zi zi new Zi();zi.methodZi();zi.methodFu();//继承zi.method();//子类中的method方法} } 二重写方法的注意事项 1.子类重写父类方法之后,权限必须要保证大于等于父类权限(权限指的是访问权限)   public - protected - 默认 - private例子如下去掉method前面的public将会报错 但父类去掉一点事没有 2.子类方法重写父类方法,方法名和参数列表要一样 3.私有方法不能被重写,构造方法不能被重写,静态方法不能被重写 静态方法 私有方法 构造方法 4.子类重写父类方法之后,返回值类型应该是父类方法返回值类型的子类类型   这样返回的是父类方法返回值类型的子类类型 相反就不可以 三方法重写的使用场景 1.使用场景:功能升级改造,子类需要对父类中已经实现好的功能进行重新改造 比如对旧手机的来电显示进行改造假设老年机只能显示电话号码现在升级可以显示来电人和归属地 创建老手机 public class Oldphone {public void call(){System.out.println(打电话);}public void message(){System.out.println(发短信);}public void show(){System.out.println(显示手机号);} } 新手机 public class Newphone extends Oldphone{Overridepublic void show(){System.out.println(显示手机号);System.out.println(显示归属地);System.out.println(显示来电人);} } 测试类 public class test {public static void main(String[] args) {Newphone newphone new Newphone();newphone.call();//继承newphone.message();//继承newphone.show();} } 结果 四super和this 1.继承中构造方法的特点 1.注意:new子类对象时,会先初始化父类(先走父类无参构造方法) 2.原因:   每个构造方法的第一行,默认都会有一个super(),不写jvm自动提供一个   super()代表的是父类无参构造 例子依旧三个类 public class Fu {public Fu(){//super();System.out.println(我是父类中的无参构造);} } public class Zi extends Fu{public Zi(){//super();System.out.println(我是子类中的无参构造);}public Zi(int i){//super();System.out.println(我是子类中的有参构造);} } 测试 public class test {public static void main(String[] args) {Zi zi new Zi();System.out.println();Zi zi1 new Zi(10);} } 结果 2.super和this的具体使用 super的具体使用 1.概述:代表的是父类引用 2.作用:可以调用父类中的成员 3.使用:   a.调用父类构造方法- 在子类中的构造中写     super() - 调用父类无参构造     super(实参)  - 调用父类有参构造          b.调用父类成员变量:     super.成员变量名            c.调用父类成员方法:     super.成员方法名(实参) //直接调用方法里的num遵循就近原则 三个类 public class Fu {int num 10;public Fu(){System.out.println(我是父类中的无参构造);}public Fu(int data){System.out.println(我是父类中的有参构造);}public void method(){System.out.println(我是父类中的method方法);} }public class Zi extends Fu{int num 100;public Zi(){super();//调用父类中的无参构造System.out.println(我是子类中的无参构造);}public Zi(int num){super(10);//调用父类的有参构造System.out.println(我是子类中的有参构造);}public void method(){super.method();//调用父类的method方法System.out.println(我是子类中的method方法);System.out.println(num);//子类自己的System.out.println(super.num);//调用父类的num} } 测试类 public class Test01 {public static void main(String[] args) {Zi zi new Zi();System.out.println();Zi zi1 new Zi(10);System.out.println();Zi zi2 new Zi();zi2.method();} } 前面加了super就可以直接调用父类引用结果 this的具体使用 1.this概述:代表的是当前对象(哪个对象调用的this所在的方法,this就代表哪个对象) 2.作用:   a.区分重名的成员变量和局部变量   b.调用当前对象中的成员 3.使用:   a.调用当前对象的构造:在构造中写     this():调用当前对象的无参构造     this(实参):调用当前对象的有参构造   b.调用当前对象的成员变量:     this.成员变量名   c.调用当前对象的成员方法:     this.成员方法名(实参) 4.注意:   不管是super还是this,只要在构造中使用,都必须在第一行,所以二者不能同时手写出来 创建一个Person类 现在调用里面的有参/无参构造那就在无参/有参构造里面加一个this //直接调用方法里的num遵循就近原则 public class Person {int num 10;public Person(){//this(10);System.out.println(我是Person中的无参构造);}public Person(int data){//super();super和this不能同时再构造中出现this();System.out.println(我是Person中的有参构造);}public void method(){int num 20;System.out.println(num);//20System.out.println(this.num);//10this.method01();System.out.println(我是Person类中的method方法);}public void method01(){System.out.println(我是Person类中的method01方法);} }测试类 public class Test01 {public static void main(String[] args) {Person person new Person();System.out.println();Person person1 new Person(10);System.out.println();Person person2 new Person();person2.method();} } 结果
http://www.hkea.cn/news/14469191/

相关文章:

  • 做网站推广链接该怎么做扬中新闻中心
  • wordpress站点搬家网页设计基础成果介绍
  • 网站域名注销流程商丘做网站优化
  • 可以做一键拨号和导航的网站有关网站排名的论文
  • 手机网站免费生成app专业简历制作公司
  • 武威 网站建设公司网站一般多少钱
  • 提供扬中网站建设制作网站作品
  • 大一网页设计个人网站代码基础建设龙头股
  • 苏州网站搜索引擎优化哪个网站可以做优惠券
  • 怀宁网站建设婚纱摄影网站模板下载
  • 南京做网站哪家最好成都个人网站
  • google外贸建站株洲网站建设制作
  • 静海的做网站南宁开发公司
  • 网站域名到期什么意思做网站安全的公司有哪些
  • wordpress下载的主题如何安装郑州网站seo优
  • 多少钱做网站wordpress+中国+论坛
  • wordpress网站收录做网站优化的价格
  • 十堰网站建设价格哈尔滨市信息网
  • 上海企业网站模板建站镇海网站建设
  • 外网怎样访问自己做的网站镇平建设局网站
  • 建设网站最新动态济南官网seo厂家
  • 河南省工程建设业协会网站wordpress数据搬移
  • 网站建设蓝图ppt桂阳网站定制
  • 宠物网站设计模块职业技能培训机构
  • logo素材库网站免费河南濮阳建设局网站
  • linux空间做网站南宁企业网站制作
  • 建设银行 钓鱼网站国内免费注册二级域名的网站
  • wordpress集成微信支付win7优化教程
  • 凡科网站建设多少钱seo和sem的区别是什么?
  • 快捷网站建设梅州南站