Java――类的应用(二) (三)

2014-11-24 11:52:32 · 作者: · 浏览: 72
mul的具体实现

public int umul(int a, int b) {

return a / b;

}

}

public class test {

public static void main(String[] args) {

test03 t3 = new test03();

System.err.println("a + b = " + t3.add(3, 2));

System.err.println("a - b = " + t3.sub(3, 2));

System.err.println("a * b = " + t3.mul(3, 2));

System.err.println("a / b = " + t3.umul(3, 2));

}

}


如果实现接口的类时抽象类,那该怎么办呢?很简单,它可以将其推给第一个具体子类。其子类可以完成父类的任务,重写接口和父类中的抽象方法,提供具体实现。下面是一个抽象类实现接口的实例:

[java]
interface ladd {

void add(int a , int b);

}

//抽象类实现了接口

abstract class test02 implements ladd{

//提供了add()方法的具体实现,也可以不提供

public void add(int a , int b){};

//定义了一个抽象的方法

abstract void sub(int a , int b);

}



class test03 extends test02{



@Override

public void add(int a, int b) {

System.out.println("a + b" + (a + b));

}

@Override

void sub(int a, int b) {

System.err.println("a - b" + (a - b));

}

}



public class test {

public static void main(String[] args) {

test03 t3 = new test03();

t3.add(2, 3);

t3.sub(2, 3);

}

}

interface ladd {

void add(int a , int b);

}

//抽象类实现了接口

abstract class test02 implements ladd{

//提供了add()方法的具体实现,也可以不提供

public void add(int a , int b){};

//定义了一个抽象的方法

abstract void sub(int a , int b);

}

class test03 extends test02{

@Override

public void add(int a, int b) {

System.out.println("a + b" + (a + b));

}

@Override

void sub(int a, int b) {

System.err.println("a - b" + (a - b));

}

}

public class test {

public static void main(String[] args) {

test03 t3 = new test03();

t3.add(2, 3);

t3.sub(2, 3);

}

}
一个类实现多个接口,这就需要它为接口中的抽象方法提供具体的实现,如果这个接口继承了另一个接口,那么这个类也需要实现它所实现的子接口的付接口的抽象方法。

[java]
interface ladd {

void add(int a , int b);

}

//接口继承实现了另一接口

interface test02 extends ladd{

void sub(int a , int b);

}



class test03 implements test02{

@Override

public void sub(int a, int b) {

}



@Override

public void add(int a, int b) {

}

}

interface ladd {

void add(int a , int b);

}

//接口继承实现了另一接口

interface test02 extends ladd{

void sub(int a , int b);

}

class test03 implements test02{

@Override

public void sub(int a, int b) {

}

@Override

public void add(int a, int b) {

}

}


4)、如何引用接口

在Java中可以建立接口类型的引用变量,接口的引用变量能够存储一个指向对象的引用值,这个对象可以是任何实现该接口的类的实例。通过接口引用可以调用对象的方法,这些方法必须是类中的抽象方法。下面是通过程序说明如何引用接口:

[java]
interface ladd {

int add(int a , int b);

}

interface lsub {

int sub(int a , int b);

}

interface lmul {

int mul(int a , int b);

}

interface lumul {

int umul(int a , int b);

}



class test03 implements ladd , lmul , lsub , lumul {

@Override

public int add(int a, int b) {

return a + b;

}

@Override

public int mul(int a, int b) {

return a - b;

}

@Override

public int sub(int a, int b) {

return a * b;

}

@Override

public int umul(int a, int b) {

return a / b;

}

}



public class test {

public static void main(String[] args) {

test03 t3 = new test03();

//接口引用指向对象的引用

ladd bb = t3;

lsub cc = t3;

lmul dd = t3;

lumul ee = t3;

//对象引用调用add()方法

System.err.println("a + b= " + t3.ad