只有秃头才能变强java
package Demo1;
//使用继承后,猫和狗都属于动物类,都要吃和叫
//因此创建一个动物类
class Animals{
public void eat() {
System.out.println("吃");
}
public void barking() {
System.out.println("叫");
}
}
//猫和狗都继承动物类
class Cat extends Animals{}
class Dog extends Animals{}
public class ExtendsDemo {
public static void main(String[] args) {
//建立一只小狗,并调用eat和barking方法
Dog dog=new Dog();
dog.eat();
dog.barking();
//建立一只小猫,并调用eat和barking方法
Cat cat=new Cat();
cat.eat();
cat.barking();
}
}
复制代码
运行结果app
吃
叫
吃
叫ide
//Java只支持单继承,不支持多继承
class Father{}
class Mother{}
//正确的写法
class Son extends Father{}
//错误的写法
class Son extends Father and Mother{}
复制代码
package Demo1;
//Java支持多重继承
class GrandFather {
public void say() {
System.out.println("我是你爷爷");
}
}
class Father extends GrandFather {
public void show() {
System.out.println("我是你爸爸");
}
}
class Son extends Father {
}
public class ExtendsDemo {
public static void main(String[] args) {
// 建立一个儿子对象
Son son = new Son();
// 调用父亲的方法
son.show();
// 调用爷爷的方法
son.say();
}
}
复制代码
运行结果:this
我是你爸爸
我是你爷爷spa
package Demo1;
class Father {
private int num = 10;
public int num1 = 20;
// 私有方法,子类不能继承
private void show() {
// num能够在父类中访问
System.out.println(num);
System.out.println(num1);
}
public void say() {
System.out.println(num);
System.out.println(num1);
}
}
class Son extends Father {
public void funtion() {
// 子类不能继承父类的私有成员
// System.out.println(num);
System.out.println(num1);
}
}
public class ExtendsDemo {
public static void main(String[] args) {
// 建立对象
Son son = new Son();
// s.show(),子类不能继承父类的私有成员方法
son.say();
son.funtion();
}
}
复制代码
输出结果
10
20
20设计
继承中成员变量的关系,当子类中成员变量与父类中的成员变量名称同样时,在子类方法中访问一个变量的查找顺序:code
package Demo1;
class Father {
public Father() {
System.out.println("Father的无参构造");
}
public Father(String name) {
System.out.println("Father的有参构造");
}
}
class Son extends Father {
public Son() {
//super(),子类的每一个构造方法第一条语句默认super()
System.out.println("son的无参构造");
}
public Son(String name) {
//super()
System.out.println("son的有参构造");
}
}
public class ExtendsDemo {
public static void main(String[] args) {
// 建立对象
Son son = new Son();
System.out.println("***************");
Son son2=new Son("tom");
}
}
复制代码
输出结果
Father的无参构造
son的无参构造orm
Father的无参构造
son的有参构造视频
从运行结果能够看出,每次建立一个子类对象都会访问父类的无参构造方法,当父类中没有无参构造方法时,项目会报错,解决方法:对象
package Demo1;
class Father {
/*
* public Father() { System.out.println("Father的无参构造"); }
*/
public Father(String name) {
System.out.println("Father的有参构造");
}
}
class Son extends Father {
public Son() {
super("John");
System.out.println("son的无参构造");
// super("John");
}
public Son(String name) {
// super("Jimmy");
this();
System.out.println("son的有参构造");
}
}
public class ExtendsDemo {
public static void main(String[] args) {
// 建立对象
Son son = new Son();
System.out.println("***************");
Son son2 = new Son("tom");
}
}
复制代码
输出结果
Father的有参构造
son的无参构造
Father的有参构造
son的无参构造
son的有参构造
package Demo1;
class Laptop extends Pc{
public void Read(String name) {
super.Read(name);
System.out.println("我能够看视频");
}
}
class Pc{
public void Read(String name) {
System.out.println("我能够看小说");
}
}
public class ExtendsDemo1 {
public static void main(String[] args) {
//建立对象
Laptop lt=new Laptop();
lt.Read("哈利波特");
}
}
复制代码
输出结果:
我能够看小说
我能够看视频
浑浑噩噩一天又过去了