一.高级类的特征
1.继承(简而言之呢就是在父类中写其它子类的共有属性,特有属性在子类中编写即可)
就像需求:
人类{
姓名
性别
年龄
打印信息方法
}
学生{
姓名
性别
年龄
学校
打印信息方法
}
教师{
姓名
性别
年龄
科目
打印信息方法
}
工人{
姓名
性别
年龄
工种
打印信息方法
}
具体实现方法
父类编写:
public class Person { int age; String name; int sex; public void showInfo(){ System.out.println(this.age); System.out.println(this.name); System.out.println(this.sex); } } 以学生类为例子其余以此类推(extends来继承父类的所有属性)
public class Student extends Person{ String school; public void showInfo(){ System.out.println(this.age); System.out.println(this.name); System.out.println(this.sex); System.out.println(this.school); } } 注(不能因为简便把不同种类的写继承比如把狗的一类当成人类的子类!!!需要重新写一个动物的类不能是private)只能单继承【层层嵌套不能有多个父类】
练习1:
1.(1)定义一个ManKind类包括成员变量 int sex ,int salary 方法void manOrWoman()根据sex(0.1用这个来判断男女),void employeed()根据salary的有无来判断no job 或者job
(2).定义Kinds继承ManKind并包括成员变量int yearsOld;方法printAge()打印yearsOld的值
(3)在kinds类的man方法中实例化Kinds对象somekind,用改变量访问父类
父类代码如下
public class ManKind { int sex; int salary; public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public void manOrWoman() { if (this.sex == 1) { System.out.println("man"); } else if (this.sex == 0){ System.out.println("woman"); } } public void employeed(){ if (this.salary == 0) { System.out.println("no job"); } else{ System.out.println("job"); } } }
子类如下:
public class Kinds extends ManKind { int yearsOld; public int getYearsOld() { return yearsOld; } public void setYearsOld(int yearsOld) { this.yearsOld = yearsOld; } public void printAge(){ System.out.println(this.yearsOld); } public static void main(String[] args) { Kinds someKis=new Kinds(); someKis.setSex(0); someKis.setSalary(100); someKis.manOrWoman(); someKis.employeed(); } }
子类调用输入后输出结果:
二.方法的重写(idea中方法重写快捷键ctrl+o)
子类重写父类的方法,只是重写编写方法体的代码,如果父类的方法是public的,其他重写不能更严格或者不写!(子类不能访问父类私有的东西)
我们以前面的父类Person 和子类 Student为例
public class Person { int age; String name; int sex; public void showInfo(){ System.out.println(this.age); System.out.println(this.name); System.out.println(this.sex); } }
当没有重写的时候
public class Student extends Person{ String school; public static void main(String[] args){ Student stu= new Student(); stu.showInfo(); } } 输出结果为
下面是重写后的
public class Student extends Person{ String school; @Override public void showInfo() { System.out.println("以下是student类对Person类showInfo方法的重写"); System.out.println(this.age); System.out.println(this.name); System.out.println(this.sex); } public static void main(String[] args){ Student stu= new Student(); stu.showInfo(); } }
输出结果为:
Override的例题修改例题中的kinds在kinds中重新定义emp;oyeed()方法,覆盖父类中定义的方法,输出“kinds should study and no job”;
改写位置及代码如下
@Override public void employeed() { System.out.println("kinds should study and no job"); }
输出结果:
三.四种访问修饰符访问权限
补充:1.如果子类和父类在同一个包下,那么对于父类的成员修饰符只有不是私有的private,那就都可以使用
2.如果子类和父类不在同一包下,子类只能使用父类中protected和public修饰的成员
四.关键字super
1.在java类中使用super来调用父类中的指定操作(1【用于访问访问父类定义的属性】2【调用父类中定义成员方法】3【在子类构造方法中调用父类的构造器】)
注意:尤其当父类中出现同名成员时,可以用super进行区分,super的追溯不仅限于直接父类,super和this的用法相似,this代表本类对象的引用,super代表父类内存空间的标识。
练习(在练习定义kinds的employeed()方法中调用父类的mankind的这个方法让后输出but kinds should study and no job)
@Override public void employeed() { super.employeed(); System.out.println("but kinds should study and no job"); } 五.调用父类的构造器
1.子类中所有的构造器默认都会访问父类中空参数的构造器
父类
public ManKind(){ System.out.println("Mankinds"); }
子类
public static void main(String[] args) { Kinds someKis=new Kinds();}//默认调用无参构造方法这个过程中访问到父类的无参构造
显示
2.当父类中没有空参数的构造器时,子类的构造器必须通过this参数列表或者super参数列表语句指定调用本类或者父类中相对应的构造器,且必须放在构造器第一行(父类只有有参子类必须显示构造一个构造来调用父类)
3.如果子类的构造器中既未显示调用父类或本类的构造器,且父类中又没有无参数构造器,则编译出错。
六.this和super的区别
区别点 this super
访问属性 访问本类中的属性,如果 访问父类中的属性
没有此属性则从父类中继续查找
调用方法 访问本类中的方法 直接访问父类中的方法
调用构造器 调用本类构造器必须 调用父类构造器必须放在子类
放在构造器的首行 构造器首行
特殊 表示当前对象 无此概念
七.面向对象的特征:多态性
1.方法的重载(本类中同名方法体现相同名称方法实现不同逻辑)和重写(子类对父类方法的覆盖子类可以使用和父类相同名的方法名,覆盖父类的逻辑父类的方法想改逻辑但是有别的代码在调用父类的算法可以考虑用子类继承父类,重新父类的方法)
2.对象的多态性可以直接应用在抽象类和接口上
3.java引用变量有两个类型:编译时类型和运行时类型。编译类型由声明改变量使用的类型决定 ,运行时类型由实际赋给改变量的对象决定(若编译时类型和运行时类型不一致,就会出现态性)
小结:需要存在继承或者实现关系 需要有覆盖操作
成员方法(成员方法的多态性也就是动态绑定,必须得存在于方法的重写之上):编译时要查看引用变量所属的类中是否有所调用的方法 。运行时调用实际对象所属的类中的重写方法。
成员变量:不具备多态性,只看引用变量所属的类。
八.instanceof操作符 x instanceof A检验x是否为A的对象,返回值为boolean
Student s=new Student(); Person e=new Student(); Person p= new Person(); // p=new Student(); s.showInfo(); p.showInfo(); e.showInfo();//调用的是student的 System.out.println(s instanceof Person); System.out.println(p instanceof Person); System.out.println(p instanceof Student); System.out.println(e instanceof Student);
输出为
九.Object类(是所有java类的根父类【基类】)
如果在类的声明中未使用extends关键字声明其父类,则默认为Object类
主要方法:1.public Object()构造方法 2.System.out.println(p.equals(e));对象比较
3.获取hash码System.out.println(o.hashCode());
4.对象打印时调用System.out.println(p.toString());
public class Test { //给test方法设置一个形参参数,这个参数我不确定到底可以传进来一个什么类型 // 可以确定的是传递实参一定是一个类,那么test的方法的形参要设计一个什么类型 public void test(Object obj){ } public static void main(String[] args){ Test t=new Test(); Person p=new Person(); Student s=new Student(); t.test(p); t.test(s); t.test(new Kk()); Person e=new Person(); System.out.println(p.equals(e)); Object o=new Object(); System.out.println(o.hashCode()); System.out.println(p.toString()); } }
输出结果:
今日的学习也暂时先告一段落了,快乐明天继续延续!!!摆了个白哦!