继承
继承概述
- 继承可以使得子类具有父类的方法,还可以在子类中重新定义,追加属性和方法
- 格式: public class 子类名 extends 父类{}
- 如: public class ZI exends FU
- 子类可以有父类的内容
- 子类也可以有自己特有的内容
package Demo1;
//测试类
public class Demo {
public static void main(String[] args) {
//创建对象,调用方法
FU f = new FU();
f.show();
ZI z = new ZI();
z.method();
z.show();
}
}
package Demo1;
public class ZI extends FU {
public void method(){
System.out.println("method方法被调用");
}
}
package Demo1;
public class FU {
public void show(){
System.out.println("show方法被调用");
}
}
- 继承中如果子类有变量就不变量
- 在方法中-——>在子类中——>在父类中
super
- this: 代表本类对象的使用
- super:代表父类对象的使用
关键字 | 访问成员变量 | 访问构造方法 | 访问成员方法 |
---|---|---|---|
this | this.成员变量 | this(…)访问本类构造方法 | this.成员方法,访问本类成员方法 |
super | super.成员变量 | super(…)访问父类构造方法 | super.成员方法(),访问父类成员方法 |
package Demo1;
//测试类
public class Demo {
public static void main(String[] args) {
//创建对象,调用方法
ZI z =new ZI();
z.show();
}
}
package Demo1;
public class ZI extends FU {
public int age = 20 ;
public void show(){
int age = 30 ;
//局部变量
System.out.println(age);
//我要访问本地类变量,怎么办呢?
System.out.println(this.age);
//我要访问父类成员变量,怎么办呢?
System.out.println(super.age);
}
}
package Demo1;
public class FU {
public int age = 18;
public void show(){
System.out.println("show方法被调用");
}
}
- 继承的特点
子类中所有的构造方法默认都会访问父类中的无参构造方法
为什么呢?
- 子类初始化前,一定都先完成父类数据初始化
- 每一个子类构造方法第一条默认语句都是super();
如果父类中没有无参构造方法怎么办?
- 通过super调用父类带参构造方法
- 在父类中自己定义一个无参构造方法
package Demo1;
//测试类
public class Demo {
public static void main(String[] args) {
//创建对象,调用方法
ZI z =new ZI();
ZI z1 = new ZI(18);
}
}
package Demo1;
public class FU {
public FU(){
System.out.println("FU中无参构造方法被调用;" );
}
public FU(int age){
System.out.println("FU中带参方法被调用");
}
}
package Demo1;
public class ZI extends FU {
public ZI(){
//System.out.println("ZI中无参构造方法被调用");
}
public ZI(int age){
super(20);
System.out.println("ZI中父类带参构造方法被调用");
}
}
方法重写
- 概述:子类中出现了和父类中一模一样的方法声明
- 方法重写的应用:当子类需要父类的功能,而功能主体子类有自己特有的内容,可以重写父类中和方法,这样,即沿袭了父类的功能,又定义了子类独的内容
package Demo2;
public class PhoneDemo {
public static void main(String[] args) {
//创建对象,调用方法
Phone p = new Phone();
p.call("pofenx");
System.out.println("----------");
NewPhone p1 = new NewPhone();
p1.call("pofenmax");
}
}
package Demo2;
public class NewPhone extends Phone{
@Override//注解,检验方法重写声明的正确性。
public void call(String name){
System.out.println("开启视频功能");
super.call(name);//调用父类功能
}
}
package Demo2;
public class Phone {
public void call(String name){
System.out.println("给"+name+"打电话");
}
}
- 注意:子类方法重写父类方法的时候子类的访问权限不能比父类低(public>默认>私有)
- 私有方法不能被重写
java 继承注意
- java中类只支持单继承,不支持多继承
- java中类支持多层继承
猫和鼠
package Demo2;
public class Animal {
private String name;
private int age ;
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 Animal(String name , int age){
this.name = name ;
this.age = age;
}
public Animal() {
}
}
package Demo2;
public class Dog extends Animal{
public Dog(){}
public Dog(String name ,int age){
super(name , age);
}
public void lookDoor(){
System.out.println("看门");
}
}
package Demo2;
public class Cat extends Animal{
public Cat(){}
public Cat(String name ,int age){ super(name,age);}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
package Demo2;
public class Application {
public static void main(String[] args) {
Dog a = new Dog("小白",18);
Cat b = new Cat("小黑",18);
System.out.println(a.getName()+","+a.getAge());
a.lookDoor();
System.out.println(b.getName()+","+b.getAge());
b.catchMouse();
}
}
包
- 包其实就是一个文件夹
- 作用:对类进行分类和管理
- 格式 package 包名(多级包.分开)
- 范例 package.com.pofen(包名);
- 手动建包
- 自动建包:Javac -d.Helloword.java 范例:java.com.www.HelloWorld
导包 import
格式:import 包;
范例:import cn.www.Teacher
final
- 修饰方法:表明该方法是最终方法,不能被重写
- 修饰变量:表明是常量:不能被再次赋值
- 修饰类:表明最终类,不能被继承
static
- static是静态的医生,可以修饰成员方法,成员变量
- static修饰特点
- 被类的所有对象共享(这也是我们判断是否使用静关键字的条件)
package Demo3;
public class Application{
public static void main(String[] args) {
Student.university = "yunnandc";
Student s1 = new Student();
s1.name="pofenx";
s1.age= 20;
Student s2 = new Student();
s2.age = 21;
s2.name= "pofenx";
s1.show();
s2.show();
}
}
package Demo3;
public class Student{
public String name;
public int age;
public static String university;
public void show(){
System.out.println(name+","+age+","+","+university);
}
}
- 静态成员方法只能访问静态成员
多态
同一个对象,在不同时刻表现出来的不同形态
example:
我们可以说猫是猫 猫 cat = new 猫();
我们也可说猫是动物 动物 animal = new 猫();
多态的前提提现
- 有继承/实现关系
- 有方法重写
- 有父类引用指向子类
package Demo4;
public class Animal {
public void eat(){
System.out.println("动物吃东西");
}
}
package Demo4;
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package Demo4;
public class Application {
public static void main(String[] args) {
Animal a = new Cat();
}
}
多态中成员访问特点
- 成员变量:编译看左边,运行看左边
- 成员方法:编译看左边,运行看右边
- 为什么?:成员方法有重写,而成员变量没有
package Demo4;
public class Animal {
public int age = 40;
public void eat(){
System.out.println("动物吃东西");
}
}
package Demo4;
public class Cat extends Animal{
public int age = 20;
public int weight = 10;
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void palyGame(){
System.out.println("猫捉迷藏");
}
}
package Demo4;
public class Application {
public static void main(String[] args) {
//有父类指向子类对象
Animal a = new Cat();
System.out.println(a.age);
// System.out.println(a.weight);
a.eat();
//a.playGame();
}
}
40
猫吃鱼