Java interface

Java interface的使用

在使用接口定义需要注意几点:
1. 每个interface的所有方法的权限为public。
2. 实现接口的非抽象类必须要实现该接口的所有方法。抽象类可以不用实现。

Which should you use, abstract classes or interfaces?

Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.

package com.hotmail.henrytien;

interface Inter1{
    public static String color = "Red";
    public void show();
}
interface Inter2{
    public void play();
            }

class A implements Inter1,Inter2{
    @Override
    public void show() {
        System.out.println("show");
    }
    @Override
    public void play() {
        System.out.println("play");
    }
}

public class InterFaceLearn {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        A i = new A();
        i.show();
        i.play();
        System.out.println(Inter1.color);
    }
}

output:
show
play
Red


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值