Java课件第四章

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Athlete Amigo=new Athlete(); Amigo.run(); Amigo.jump(); } }
6
4.1 接口的定义
【例2】定义一个沙发接口继承多个接口。
interface Sittable {
void sit(); } interface Lie {
void sleep(); } interface Sofa extends Sittable,Lie {
interface Math
{
double PI=3.1415926;
}
class Arithmetic implements Math
{
double roundArea(double radius)
{
return PI*radius*radius;
}
}
class Student {
public static void main(String[] args)
5
class Athlete implements Sport {
public void run() {
System.out.println(“短跑”); } public void jump() {
System.out.println(“三级跳”); } public static void main(String[] args) {
public void adjust() {}
}
15
class Brass5 extends Wind5 {
public void play() {
System.out.println("Brass5.play()");
}
public void adjust() {
System.out.println("Brass5.adjust()");
间的层次关系; 可以指明多个类需要实现的方法; 可以了解对象的编程界面,而不需了解对象所对
应的类(揭示一个编程界面,而不揭示类体); 通过一个接口列表实现多重继承(多重继承的伪
装) 。
2
4.1 接口的定义
Interface totally abstract class
This is what all classes that implement this particular interface will look like.
class C2 implements I1,I2{

public void f(){System.out.println("I1.f()");}
口 中}
public int f(int i){System.out.println(i); return 2;}

class C3 extends C implements I2{ public int f(int i){System.out.println(i);
3
实现interface:An class implements an interface
to have the interface. 格式:class className implements BaseInterfaces
实现interface的类,其interface中所有的方法 必须被“实现”,否则这个类成为一个抽象类。

public static void main(String[] args)

{ C2 a=new C2(); C3 b=new C3();
C4 c=new C4();
C d=new C();
a.f(); // a.f(2);
b.f(); // b.f(3);
c.f();
}}
d.f();
Case: Ctest
{
System.out.println(“Dmeng’s VideoCard is working”);
}
public String getName()
{
return name;
}
public Dmeng()
{
name=“Dmeng’s VideoCard”;
}
public void setName(String name)
public interface InterfaceName extends BaseInterfaces 在interface中所有的方法都是public abstract的,即使 你没有声明它是public abstract的。不能使用static、 final、private、protected修饰符。 在interface中所有的数据成员都是public static final的, 即使你没有声明.但不能是blank final。
第四章 Java类和对象的高级特征
4.1 接口
4.1.1 定义接口 4.1.2 实现接口
4.2 内部类
4.2.1 内部类特性 4.2.2 静态内部类
4.3 包
4.3.1 Java的API介绍 4.3.2 引用Java定义的包 4.3.3 自定义包
1
4.1 接口的定义
接口---方法定义和常量值的集合 定义不相关类的共同行为,而不需考虑这些类之
{
Arithmetic a=new Arithmetic();
System.out.println(a.roundArea(3));
System.out.println(Math.PI);
System.out.println(Arithmetic.PI);
System.out.println(a.PI);
interface 可以从多个interface得到继承,但是不能继承类: Case Study: HorrorShow.java
一个类可以实现多个接口: Case Study: Adventure.java
17
interface Product { static final String MAKER = "My Corp"; static final String PHONE = "7963628"; public int getPrice(int id);
}
public void adjust() {}
class Stringed5 implements Instrument5 {
public void play() {
System.out.println("Stringed5.play()"); }
public String what() { return "Stringed5"; }
this.vc=vc;
}
void run( )
{ System.out.println(strCpu);
System.out.println(vc.getName());
vc.Display();
System.out.println(“Mainboard is running”)
}
}
11
装配电脑
class Computer {
}
}
Case: Student
13
interface I1{void f();}
interface I2{int f(int i);}
interface I3{int f();}
不 同
class C{public int f(){System.out.println("C.f()"); return 1;} }
class Percussion5 implements Instrument5 {
public void play() {
}
System.out.println("Percussion5.play()");
public String what() { return "Percussion5"; }
static void tune(Instrument5 i) {
i.play();
}
static void tuneAll(Instrument5[] e) {
for(int i = 0; i < e.length; i++)
tune(e[i]);
}
public static void main(String[] args) {
Instrument5[]Байду номын сангаасorchestra = new Instrument5[5];
int i = 0;
orchestra[i++] = new Wind5();
orchestra[i++] = new Percussion5();
orchestra[i++] = new Stringed5();
public static void main(String[] args) {
Dmeng d=new Dmeng(); Mainboard m=new Mainboard(); m.setCpu(“Intel’s cpu”); m.setVideoCard(d); m.run(); } }
12
【例5】interface中的数据成员。
8
4.1 接口的定义
【例4】定义标准显卡接口实现主板和显卡的通信。
显卡接口
interface VideoCard {
void Display(); String getName(); }
9
显卡厂商的实现
class Dmeng implements VideoCard
{
String name;
public void Display()
14
Case: import java.util.*;
Music5
interface Instrument5 { int i = 5; // static & final
void play(); // Automatically public
String what();
void adjust(); }
class Wind5 implements Instrument5 {
public void play() {
}
System.out.println("Wind5.play()");
public String what() { return "Wind5"; }
}
public void adjust() {}
}
7
4.1 接口的定义
【例3】定义一个沙发类来实现多个接口的方法。
interface Sittable {
void sit(); } interface Lie {
void sleep(); } class Sofa implements Sittable,Lie {
public void sit(){} public void sleep(){} }
名 方
}
return 3;}
class C4 extends C implements I3{

public int f(){System.out.println("I1.f()");
的}
return 1;}
继 承
//class C5 extends C implements I1{} //interface I4 extends I1,I3{} public class Ctest{
所有实现interface中的方法必须被声明为 public.
interface可以从多个interface得到继承,但是 不能继承类.
一个类可以实现多个interface.
4
4.1 接口的定义
【例1】定义Sport接口,运动员来实现接口中的方法。
interface Sport {
void run(); void jump(); }
orchestra[i++] = new Brass5();
orchestra[i++] = new Woodwind5();
tuneAll(orchestra);
} }
Case: Music516
4.1 接口的定义
所有实现interface中的方法必须被声明为public: Case Study: Store.java
{
=name;
}
}
10
主板厂商的实现
class Mainboard
{
String strCpu;
VideoCard vc;
void setCpu(String strCpu)
{
this.strCpu=strCpu;
}
void setVideoCard(VideoCard vc)
{
}
}
class Woodwind5 extends Wind5 {
public void play() {
System.out.println("Woodwind5.play()");
}
public String what() { return "Woodwind5"; }
}
public class Music5 {
相关文档
最新文档