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

佛山抖音seo百度seo推广

佛山抖音seo,百度seo推广,杭州网站建设公司慕枫,三水专业网站建设哪家好目录 一,方法的重写 二,重写方法的注意事项 三,方法重写的使用场景 四,super和this 1.继承中构造方法的特点 2.super和this的具体使用 super的具体使用 this的具体使用 一,方法的重写 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/470209/

相关文章:

  • 案例学 网页设计与网站建设手机百度seo快速排名
  • 江门网站建设总部电话产品推广渠道有哪些
  • 网站建设全攻略站长之家ping检测
  • 导航网站 cmsgoogle chrome谷歌浏览器
  • wordpress看其他人博客优化师是做什么的
  • 现在哪个网站还做白拿2021小说排行榜百度风云榜
  • 网站流量seo提升seo排名的方法
  • 做html网站模板下载地址网站页面布局和样式设计
  • 公司网站邮箱费用磁力宅在线搜种子
  • wordpress 缺少临时文件夹刷关键词优化排名
  • 做网站要有什么团队淘宝关键词排名查询工具
  • 开源门户网站源码宁波谷歌seo
  • wordpress+一页一屏seo关键技术有哪些
  • 学校校园网站建设实施方案精准营销的案例
  • 腾讯云服务器可以做网站可以推广发广告的app
  • seo外链友情链接网站运营推广选择乐云seo
  • 做网站 要学 什么语言网站优化公司
  • 天乐测绘网做网站吗搜索引擎广告图片
  • 湖南营销型网站建设多少钱百度关键词优化软件网站
  • 怎样给网站做关键词优化百度词条
  • 做网站哪个平台搭建网站需要什么技术
  • 做gif图的网站简述网络营销的主要方法
  • 做图网站被告seo视频网页入口网站推广
  • 做的网站底部应该标注什么意思免费文案素材网站
  • 企业网站搜索引擎拓客农夫山泉软文300字
  • 青岛黄岛区网站开发武汉seo优化
  • 东莞做网站企业铭会员制营销
  • 做网站设计工资多少钱优化教程网官网
  • 计算机网站建设与维护百度关键词统计
  • wordpress网站实现微信登录google google