建设部网站信息系统,小型网站运营,wordpress 月光博客,wordpress显示icp备案号强调#xff1a;一定用自己的话总结#xff0c;避免抄文档#xff0c;否则视为作业未完成。 this关键字的作用
为了解决成员变量和局部变量所存在的二义性,适用于有参构造时使用
示例
private String name;private int age;public person(){}public person(String name,i… 强调一定用自己的话总结避免抄文档否则视为作业未完成。 this关键字的作用
为了解决成员变量和局部变量所存在的二义性,适用于有参构造时使用
示例
private String name;private int age;public person(){}public person(String name,int age){this.name name;//不使用this关键字的话,他的值就是默认值this.age age;}为成员变量设置值, 构造器和setter方法的选择,为什么
构造器:在知道具体的参数的时候可以使用,可以创建对象并做初始化,只能使用一次
setter方法:在不知道有具体参数的时候使用,在创建对象后再使用getter方法进行调用,可以多次使用
示例:
public class person {private String name;private int age;public person(){}//无参构造器,添加有参构造器时必须写public person(String name,int age){//有参构造器this.name name;this.age age;}//getter和setter方法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 p(){System.out.println(名字:name,年龄:age);}为什么需要继承?
解决多个类中存在有相同的字段和方法的重复问题
Java中的继承语法
继承时候使用extends关键字
public class 父类名{//父类,可以编写多个不同类中相同的字段名// 存放多个子类共同的字段和方法
}public class 子类名 extends 父类名{// 存放自己特有的(独有的)字段和方法
}子类能从父类中继承到哪些成员?
1.public修饰的成员都可以被子类继承 2.private 修饰的成员子类不能继承
3.protected修饰的成员所有的子类都可以继承,专门提供给子类使用
4.默认的关键字只能是在同包下面可以被子类继承
5.有参构造器和无参构造器不能被继承
注意:java只支持单继承,也支持多重继承,不支持多继承
继承有一个根类:Objiect
子类什么时候需要覆盖父类中的方法,方法重写?(override)
在子类中添加一个 和父类一模一样的方法
//执行顺序:
//1. 先找自己是否有对应的方法,没有就找父类的
//2.找父类中是否存在该方法,如果有就执行,否则继续找
//3.知道找到objiect类,如果也没有就报错方法覆盖的规则
private修饰的方法不能被子类所继承
实例方法签名必须相同 (方法签名 方法名 方法的参数列表)子类方法的返回值类型是和父类方法的返回类型相同或者是其子类子类方法的访问权限比父类方法访问权限更大或相等 如果父类方法是private子类方法不能重写。 重写建立在继承的基础上没有继承就不能重写。
super关键字的作用
在子类中的父类方法被覆盖了,但任然需要父类中方法的东西就需要使用super关键字
示例
public class Ostrich extends Bird {public void fly() {System.out.println(自由奔跑);}//在子类中,如何调用父类中的方法public void doWork(){fly();//调用自己的fly方法//直接调用父类中的方法//super指向父类对象,只能找到直接父类super.fly();}什么时候需要把父类中的方法定义成抽象方法?
子类必须要覆盖父类中的某一个方法的时候
使用abstract修饰的方法称为抽象方法。 0
public abstract 返回类型 方法名参数;抽象方法的特点 使用abstract修饰没有方法体留给子类去覆盖 抽象方法必须定义在抽象类或接口中
使用abstract修饰的类成为抽象类。/
public abstract class 类名{}抽象类应该怎么使用?可以直接使用抽象类创建对象吗?
1.抽象类不可以创建对象,语法不支持
2.抽象类中可以同时拥有抽象方法和普通方法
3.抽象类必须要有子类,并且子类必须覆盖父类的抽象方法,否则该抽象类没有意义,或者子类也是抽象类(可以不覆盖)
抽象类的意义是什么
必须要有子类来进行继承,子类必须覆盖父类的抽象方法,除非子类也是一个抽象类就不用覆盖
Objiect中的equals和toString关键字
1.equals:
示例:
public class Student {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}//重写父类中的equalsOverridepublic boolean equals(Object o) {if (this o) return true;//地址相同就是同一个对象//o为null或者两个对象的类型不同,则不是同一个对象if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;//若果年龄相同并且姓名的值相同则表示同一个对象return age student.age Objects.equals(name, student.name);}//省略setter方法
public static void main(String[] args) {Student s1new Student(国辉,23);Student s2new Student(国辉,23);//,基本数据类型比较值,引用类型比较值System.out.println(s1 s2);//false//objiect类中的eqauls方法比较两个对象的地址,作用和相同//在Student类中重写equals方法之后,此时比较两个对象的值是否相同System.out.println(s1.equals(s2));//true}2.toString
示例:
public class Student {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}//重写toStringOverridepublic String toString() {return Student{ name name \ , age age };}//省略setter方法 public static void main(String[] args) {Student s1new Student(国辉,23);//cn.wolfcode._07_objiect.Student:全限定名//157f12c:地址//objiect类中的toString://重写objiect的toString方法,打印对象的字段值System.out.println(s1.toString());//不具有打印功能,只做字符串转换System.out.println(s1);} 文章转载自: http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.hcszr.cn.gov.cn.hcszr.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn