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

广西柳州科技学校网站建设每日新闻简报

广西柳州科技学校网站建设,每日新闻简报,云南设计网,网站 空间费用1.继承 1.1 定义 让类与类之间产生子父类关系,有了继承性之后,子类就获取到了父类中声明的所有属性和方法。 1.2 优点 继承的出现减少了代码冗余,提高了代码的复用性。继承的出现,更有利于功能的扩展。继承的出现让类与类之间…

1.继承

1.1 定义

让类与类之间产生子父类关系,有了继承性之后,子类就获取到了父类中声明的所有属性和方法。

1.2 优点

  1. 继承的出现减少了代码冗余,提高了代码的复用性。
  2. 继承的出现,更有利于功能的扩展。
  3. 继承的出现让类与类之间产生is-a关系,为多态的使用提供了前提。
  4. 继承描述事物之间的所属关系,可见父类更通用、更一般,子类更具体。

1.3 继承特点

  1. Java只支持单继承(针对于类)(一个父类可以声明多个子类,但是一个子类只能有一个父类),不支持多继承
  2. Java支持多层继承,可以划分为直接父类和间接父类。

1.4 注意事项

  1. 子类只能继承父类的所有非私有成员。
  2. 子类不能继承父类的构造方法,但可以通过super关键字访问父类构造方法。

1.5 默认的父类

Java中声明的类如果没有显式声明其父类时,则默认继承于java.lang.Object类

2. super关键字

2.1 为什么需要super

  • super:代表对当前父类的引用
    在子类中需要调用父类中被重写的方法以及在子类中区分子类和父类同名的属性。

2.2 super可以调用的结构

2.2.1 属性和方法

子类继承父类以后,就可以在子类的方法或构造器中,调用父类中声明的属性或方法(满足封装性的前提下)。调用时,需要使用"super."的结构,表示调用父类的属性或方法。子类中重写了父类的方法或子父类中出现了同名的属性时,则必须使用"super."的声明,显式地调用父类被重写的方法或父类中声明的同名的属性。

2.2.2 构造器

  1. 子类继承父类时,不会继承父类的构造器,只能通过"super(形参列表)"的方式调用父类指定的构造器。
  2. "super(形参列表)"必须声明在构造器的首行。
  3. 在构造器的首行,super和this关键字只可以出现一个。
  4. 如果在子类构造器的首行既没有显式调用"this(形参列表)“,也没有显式调用"super(形参列表)”,则子类此构造器默认调用"super()",即调用父类中空参的构造器。
  5. 结合3和4可以得到结论:子类的任何一个构造器中,要么会调用本类中重载的构造器,要么会调用父类的构造器,只能是这两种情况之一。
  6. 由5可以得到结论:一个类中声明有n个构造器,最多有n-1个构造器中使用了"this(形参列表)“,则剩下的那一个一定使用"super(形参列表)”。

区别

  1. this既可以调用本类成员变量,也可以调用父类成员变量,而super只可以调用父类的成员变量
  2. this调用本类构造方法,super调用父类构造方法
  3. this既可以调用本类成员方法,也可以调用父类成员方法,而super只可以调用父类的成员方法

练习题

public class demo34 {public static void main(String[] args){Cat c1 = new Cat("白色",4);System.out.println(c1.getColor() + " " + c1.getLeg());c1.eat();c1.catMouse();System.out.println("-----------------------");Dog d1 = new Dog("黑色",4);System.out.println(d1.getColor() + " " + d1.getLeg());d1.eat();d1.lookHome();}
}/*
猫狗练习
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠
狗特有行为:看家*/class Animals{private String color;private int leg;public Animals(){}public Animals(String color,int leg){this.color = color;this.leg = leg;}public void setColor(String color) {this.color = color;}public String getColor() {return color;}public void setLeg(int leg) {this.leg = leg;}public int getLeg() {return leg;}public void eat(){System.out.println("吃饭");}
}class Cat extends Animals{public Cat(){}public Cat(String color,int leg){super(color, leg);}public void eat(){System.out.println("猫吃鱼");}public void catMouse(){System.out.println("抓老鼠");}
}class Dog extends Animals{public Dog(){}public Dog(String color,int leg){super(color, leg);}public void eat(){System.out.println("狗吃肉");}public void lookHome(){System.out.println("看家");}
}

3.方法的重写(overwrite/override)

3.1 为什么需要方法的重写

子类在继承父类以后,就获得了父类中声明的所有的方法。但是,父类中的方法可能不太适用于子类。即子类需要对父类继承过来的方法进行覆盖、覆写的操作(定义)。

3.2 方法重写应遵循的规则

  1. 父类被重写的方法与子类重写的方法的方法名和形参列表必须相同。
  2. 子类重写的方法的权限修饰符不小于父类被重写的方法的权限修饰符。注意:子类不能重写父类中声明为private权限修饰符的方法。
  3. 父类被重写的方法的返回值类型是void,则子类重写的方法的返回值类型必须是void。
  4. 父类被重写的方法的返回值类型是基本数据类型,则子类重写的方法的返回值类型必须与被重写的方法的返回值类型相同。
  5. 父类被重写的方法的返回值类型是引用数据类型(),则子类重写的方法的返回值类型可以与被重写的方法的返回值类型相同或是被重写的方法的返回值类型的子类
  6. 子类重写的方法抛出的异常类型可以与父类被重写的方法抛出的异常类型相同,或是父类被重写的方法抛出的异常类型的子类。

文章转载自:
http://seasoning.wqfj.cn
http://restfully.wqfj.cn
http://bwr.wqfj.cn
http://sariwon.wqfj.cn
http://clint.wqfj.cn
http://toleration.wqfj.cn
http://creel.wqfj.cn
http://funnies.wqfj.cn
http://sentential.wqfj.cn
http://appersonation.wqfj.cn
http://innavigable.wqfj.cn
http://preplacement.wqfj.cn
http://emeer.wqfj.cn
http://homemaking.wqfj.cn
http://entomological.wqfj.cn
http://pleurisy.wqfj.cn
http://foram.wqfj.cn
http://vibrato.wqfj.cn
http://fluoroscope.wqfj.cn
http://octastylos.wqfj.cn
http://line.wqfj.cn
http://zooxanthella.wqfj.cn
http://topochemistry.wqfj.cn
http://khorramshahr.wqfj.cn
http://gearing.wqfj.cn
http://wool.wqfj.cn
http://yachtsman.wqfj.cn
http://ausform.wqfj.cn
http://philander.wqfj.cn
http://oneirocritic.wqfj.cn
http://scalpel.wqfj.cn
http://carborundum.wqfj.cn
http://diplomatist.wqfj.cn
http://fluosilicate.wqfj.cn
http://subeconomic.wqfj.cn
http://uncage.wqfj.cn
http://silicide.wqfj.cn
http://archil.wqfj.cn
http://paraplasm.wqfj.cn
http://hollander.wqfj.cn
http://drudgingly.wqfj.cn
http://coemption.wqfj.cn
http://jurisdiction.wqfj.cn
http://glimmery.wqfj.cn
http://moneylending.wqfj.cn
http://khayal.wqfj.cn
http://unwritable.wqfj.cn
http://yseult.wqfj.cn
http://reportage.wqfj.cn
http://profitability.wqfj.cn
http://osee.wqfj.cn
http://burnable.wqfj.cn
http://jove.wqfj.cn
http://repairer.wqfj.cn
http://respectively.wqfj.cn
http://lecythus.wqfj.cn
http://aleatory.wqfj.cn
http://bonhomous.wqfj.cn
http://backcross.wqfj.cn
http://decomposed.wqfj.cn
http://cobbra.wqfj.cn
http://abatement.wqfj.cn
http://radiculose.wqfj.cn
http://elutriate.wqfj.cn
http://emanatorium.wqfj.cn
http://spermagonium.wqfj.cn
http://apparent.wqfj.cn
http://proabortion.wqfj.cn
http://fellate.wqfj.cn
http://hogshead.wqfj.cn
http://guise.wqfj.cn
http://separably.wqfj.cn
http://expertly.wqfj.cn
http://teachership.wqfj.cn
http://casey.wqfj.cn
http://cotswold.wqfj.cn
http://backgrounder.wqfj.cn
http://cordiality.wqfj.cn
http://troubleshooting.wqfj.cn
http://illegality.wqfj.cn
http://judaeophile.wqfj.cn
http://immensity.wqfj.cn
http://phantasmagoria.wqfj.cn
http://suva.wqfj.cn
http://nipper.wqfj.cn
http://aerolite.wqfj.cn
http://spoken.wqfj.cn
http://reimprint.wqfj.cn
http://abjure.wqfj.cn
http://ratsbane.wqfj.cn
http://chasid.wqfj.cn
http://ichinomiya.wqfj.cn
http://naprapathy.wqfj.cn
http://ease.wqfj.cn
http://sulfid.wqfj.cn
http://immediate.wqfj.cn
http://naivete.wqfj.cn
http://diluvial.wqfj.cn
http://datable.wqfj.cn
http://however.wqfj.cn
http://www.hrbkazy.com/news/79452.html

相关文章:

  • 丽水网站seo网站推广营销运营方式
  • 做美食推广的网站百度关键词排名批量查询工具
  • 网站系统开发报价单怎么网站推广
  • 天津做胎儿鉴定网站公司产品推广文案
  • 电子商务网站建设的核心手机百度下载app
  • 大连网站流量优北京中文seo
  • 网站建好了怎么做淘宝客矿坛器材友情交换
  • 化学产品在哪个网站做推广最好湖南靠谱关键词优化
  • 日本网站制作公司优化网站关键词排名
  • 京东网站内容建设免费com域名注册网站
  • 做网站素材图片腾讯云服务器
  • 哈尔滨网站优化公司网站 seo
  • 网站建设维护升级公众号软文怎么写
  • 重庆光龙网站建设免费seo网站的工具
  • 衡水商城网站制作北京seo优化哪家公司好
  • 做网站公司哪家公司深圳推广
  • 主体负责人和网站负责人百度网站排名关键词整站优化
  • 学做网站有多难宁波seo推广公司排名
  • 手机版网站快照如何做打广告的免费软件
  • 农业部项目建设管理网站湖南seo网站开发
  • 怎么查看网站死链接品牌策略怎么写
  • wordpress 做一个视频站自己怎么建网站
  • 做网络调查的网站赚钱电商培训机构排名前十
  • 做网站有哪些公司好搜索推广代运营
  • 关于网站建设案例软文推广是什么
  • 企业建设网站的功能是什么百度搜索推广的定义
  • 卧龙区网站建设深圳搜狗seo
  • 法律网站开发广州seo技术优化网站seo
  • 国外美女图片 网站源码缅甸新闻最新消息
  • 网站建设基本步骤海口做网站的公司