- 继承
- 子类的构建
- 多层继承
- 覆盖
- 用父类对象访问子类成员
- 抽象类
- 抽象类的应用
- 接口
- 利用接口实现类的多重继承
- 接口中静态方法和默认方法
- 解决接口多重继承中名字冲突问题
通过继承可以实现代码的复用,被继承的类称为父类或超类,由继承而得到的类称为子类,一个父类可以同时拥有多个子类,但一个子类只能拥有一个直接父类。
子类的构建public class Fu{ …} //父类的构建
public class Zi extends Fu{…}//子类的构建
新定义的子类可以从父类那里继承所有非私有成员作为自己的成员。
public class Fu {
int age=30;
String name="张三";
public Fu(String name1,int age1){
public Fu() {
System.out.println("fu中无参构造方法被调用");
}
public Fu(int a) {
System.out.println("fu中有参构造方法被调用");
}
}
public class Zi extends Fu{ Zi类是Fu类的子类
public int age=20;
super(age,name);//调用父类的有参构造方法
public void show() {
int age=20;
System.out.println(age);
System.out.println(this.age);//访问本类的成员变量
System.out.println(super.age);//访问父类的成员变量age
}
public Zi() {
System.out.println("Zi中无参构造方法被调用");
}
public Zi(int age) {
System.out.println("Zi中带参构造方法被调用");
}
}
子类中所有的构造方法默认会访问父类中无参的构造方法;java在执行子类的构造方法之前,会先自动调用父类中没有参数的构造方法;若要调用父类中某个特定的构造方法,要在子类的构造方法中通过super()语句来调用父类特定的构造方法.
super与this相似,都指对象,均放在构造方法的第一行,但super()是从子类的构造方法中调用父类的构造方法,this()是在同一个类内调用其他的构造方法。
若将父类成员声明为protected,而非private,则该成员不仅可以在父类中直接访问,也可以在其子类中访问。
class Aa{}
class Ba extends Aa{}
class Cc extends Ba{}
java中只允许多层继承。
覆盖在子类中定义父类中已有的方法时,应保持与父类完全相同的方法头声明,即拥有完全相同的方法名、返回值类型和参数列表。
class Person{
protected String name;
protected int age;
public Person(String name,int age){//定义Person类的构造方法
this.name=name;
this.age=age;
}
protected void show(){
System.out.println("姓名"+name+" 年龄:"+age);
}
}
class Student extends Person{//定义子类Student,其父类为Person
private String department;
public Student(String name,int age,String dep)//定义Student类的构造方法
super(name,age);
department=dep;
}
protected void show(){//覆盖父类Person中的同名方法
System.out.println("系别"+department);
}
}
public class Test{//测试
public static void main(String[] args){
Student stu=new Student("张三",20,"电子");
stu.show();
}
}
子类中不能覆盖父类中声明为final或static的方法。
用父类对象访问子类成员class Person{
protected String name;
protected int age;
public Person(String name,int age){//定义Person类的构造方法
this.name=name;
this.age=age;
}
protected void show(){
System.out.println("姓名"+name+" 年龄:"+age);
}
}
class Student extends Person{//定义子类Student,其父类为Person
private String department;
public Student(String name,int age,String dep)//定义Student类的构造方法
super(name,age);
department=dep;
}
protected void show(){//覆盖父类Person中的同名方法
System.out.println("系别"+department);
}
public void subShow(){
System.out.println("我在子类中");
}
}
public class Test{//测试
public static void main(String[] args){
Person per=new Student("张三",20,"电子");//声明父类变量per指向子类对象
per.show();//利用父类对象per调用show()方法
}
}
抽象类
Java中可以创建专门的类作为父类,这种类叫抽象类,它以abstract修饰。抽象类是不能用new运算符创建实例对象的类,但它可以作为父类被它的所有子类共享。
抽象类的语法格式:
abstract class 类名{
声明成员变量;
返回值的数据类型 方法名(参数表){
…
}
…abstract 返回值的数据类型 方法名(参数表);——抽象方法。在抽象方法里,不能定义方法体
…
}
说明:抽象方法只需要声明,不需要实现,即用“;”结尾;
当一种方法声明为抽象方法时,意味着这种方法必须被子类的方法所覆盖;抽象方法中修饰符static和abstract不能同时使用。
abstract class Shape{//定义形状抽象类Shape
protected String name;
public Shape(Shape xm){//抽象类中的一般方法,本方法是构造方法
name=xm;
System.out.print("名称"+name);
}
abstract public double getArea();//将求面积的方法声明为抽象方法
abstract public double getLength();//将求周长的方法声明为抽象方法
}
classCircle extends Shape{
private final double PI=3.14;
private double radius;
public CIircle(String shapeName,double r){//构造方法
super(shapeName);
radius=r;
}
public double getArea(){//实现抽象类中的getArea()方法
return PI*radiius*radius;
}
public double getLength(){//实现抽象类中的getLength()方法
return 2*PI*radius;
}
}
class Rectangle extends Shape{//定义继承自Shape的矩形类Rectangle
private double width;
private double height;
public Rectangle(String shapeName,double width,double height){//构造方法
super(shapeName
this.width=width;
this.height=height;
}
public double getArea(){//实现抽象类的getArea()方法
return width*height;
}
public double getLength(){//实现抽象类的getLength()方法
return 2*(width+height);
}
}
public class Main{//定义主类
public static void main(String[] args){
Shape rect=new Rectangle("长方形",6.5,10.3);//声明父类对象,指向子类对象
System.out.print(";面积="+rect.getArea());
System.out.println(";周长="+rect.getLength());
Shape circle=new Circle("圆,"10.2);//声明父类对象circle,指向子类对象
System.out.print(";面积="+circlerect.getArea());
System.out.println(";周长="+circle.getLength());
}
}
接口
接口结构与抽象类相似,本身也具有数据成员、抽象方法、默认方法和静态方法;接口的数据成员都是静态的且必须初始化;接口中除了声明抽象方法外,还可以定义静态方法和默认方法,但不能定义一般方法。
public interface Jumpping {
public abstract void jump();
}public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name,int age) {
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name=name;
}
public void setAge(int age) {
this.age=age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public abstract void eat();
}
public class Cat extends Animal implements Jumpping{//以Jumpping接口来实现Cat类
public void eat() {
System.out.println("猫吃鱼");
}
public void jump() {
System.out.println("猫可以跳高了");
}
public Cat() {
}
public Cat(String name,int age) {
super(name,age);
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Jumpping j=new Cat();//接口只能用接口中的方法
j.jump();
System.out.println("------");
Animal a=new Cat(); //抽象类调用抽象类里的方法
a.setName("加菲");
a.setAge(5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
// ((Cat)a).jump();
a=new Cat("加菲",5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
}
}
若省略interface前的public修饰符,则接口使用缺省的访问控制,即接口只能被与它同一包中的成员访问。当修饰符声明为public时,接口能被任何类的成员访问。
利用接口实现类的多重继承即一个子类可以有一个以上的直接父类,该子类可以继承它所有直接父类的非私有成员。
interface Face1{
static final double PI=3.14;
abstract doubld area();
}
interface Face2{
abstract void volume();
}
public classCylinder implements Face1,Face2{
private double radius;
private int height;
public Cylinder(double r,int h){
radius=r;
height=h;
}
public double area(){
return Pi*radius*radius;
}
public void volume(){
System.out.println("圆柱体体积="+area()*height);
}
public static void main(String[] args){
Cylinder volu=new Cylinder(5.0,2);
volu.volume();
}
}
接口中静态方法和默认方法
接口中的静态方法不能被子接口继承,也不能被实现该接口的类继承,对接口中静态方法的访问,可以通过接口名直接进行访问,即用“接口.静态方法名()”的形式调用;接口中的默认方法用default修饰符来定义,默认方法可以被子接口或被实现该接口的类所继承,但不能通过接口名直接调用,要通过接口实现类的实例进行访问,即通过“对象名.默认方法名()”的形式访问。
interface Face{
final static double PI=3.14;
public default double area(int r){
return r*r*PI;
}
abstract double volume(int r,double h);
public static String show(){
return"我是接口中的静态方法";
}
}
public class Main implements Face{
public double volume(int r,double h){
return area(r)*h;
}
public static void main(String[] args){
Syste.out.println(Face.show());
Main ap=new Main();
System.out.println("圆柱体体积为:"+ap.volume(1,2.0));
}
}
解决接口多重继承中名字冲突问题
如果一个类实现了两个接口,其中一个接口有默认方法,另一接口中也有一个名称和参数都相同的方法,此时可以在接口的实现类中提供同名方法的一个新实现,或引用其中一个父接口中的默认方法,这种引用方式称为委托某父接口中的默认方法,委托方式为:接口.super.默认方法名()。
interface Face1{//定义接口Face1
final static double PI=3.14;//定义常量
public default double area(int r){//定义与Face2中同名的默认方法
return rrPI;
}
abstract double volume(int r,double h);//声明抽象方法
}
interface Face2{//定义接口Face2
public default double area(int r){//定义与Face1中同名的默认方法
return r*r;
}
}
public class Main implements Face1,Face2{//定义主类并实现接口Face1和Face2
public double area(int r){//实现两个接口中的同名默认方法area()
return Face2.super.area®;//委托父接口Face2的area()方法
}
public double volume(int r,double h){//实现接口中的方法
return area®*h;//调用本类所实现的area()方法
}
public static void main(String[] args){
Main ap=new Main();
System.out.println(“柱体体积为:”+ap.volume(1,2.0));
}
}



