java 对象 类课件_类和对象的高级特征_Java语言程序设计_ppt_大学课件预览_高等教育资讯网...

第 4章类和对象的高级特征类和对象的高级特征

4.1 接口

4.2 内部类

4.3 包

4.1 接口日常生活中的接口,

国亲对大陆的访问与九 ·二共识

“九二共识”的意思是一个中国,各自表述。

4.1.1 定义接口接口 (interface):没有实现的方法和常量的集合。 格式 例 4.1

4.1.2 实现接口 例 4.2 例 4.3

4.1.3 类和接口的关系接口的格式

[public] interface 接口名 [extends 父接口名 1,… ]

{

[public static final] 类型 变量名 1=值 ;

… ;

[public abstract] 返回值类型 方法名 1(形参表 );

… ;

}

声明接口

interface Student_info //学生情况接口

{

int year = 2002;

int age();

void output();

}

实现接口的类

public class Stu1 implements Student_info //实现学生情况接口

{

String name;

int birth_year; //类自己的成员变量

public Stu1(String n1,int y)

{

name = n1;

birth_year = y;

}

public int age() //实现接口的方法

{

return year - birth_year;

}

public void output() //实现接口的方法

{

System.out.println(this.name +" "+ this.age()+"岁 ");

}

public static void main (String args[])

{

Stu1 s1 = new Stu1("李明 ",1980);

s1.output();

}

}

一个类实现多个接口

interface Student_scoure //学生成绩接口

{

float total();

void output();

}

public class Stu2 implements Student_info,Student_scoure

{ //实现学生情况接口、学生成绩接口

String name;

int birth_year;

float math,english,computer;

public Stu2(String n1,int y,float a,float b,float c)

{

name = n1;

birth_year = y;

math = a;

english = b;

computer = c;

}

public int age()

{

return year - birth_year;

}

public float total()

{

return math + english + computer;

}

public void output() //实现接口的方法

{

System.out.print(this.name +" "+ this.age()+"岁 ");

System.out.println(math +" "+ english +" "+ computer +" "+ total());

}

public static void main (String args[])

{

Stu2 s2 = new Stu2("李明 ",1980,90,80,70);

s2.output();

}

李明 22岁 90.0 80.0 70.0 240.0

4.1.3 类和接口的关系

① 一个类只能单继承一个父类,但可以实现多个接口。

② 一个接口可以被多个类实现。

③ 接口可以不定义任何方法声名。

④ 抽象类可以定义非抽象的方法,但接口不能定义非抽象的方法。

⑤ 接口的继承是将多个接口聚合成为一个接口。

⑥ 一个类声明实现某个接口后,必须实现该接口的全部方法,包括该接口所有的父接口。被实现的方法定义,必须与接口中的定义完全一致。

⑦ 被实现的方法必须显式地给出 public修饰。

4.2 内部类

日常生活中的内部类:

中国由四个直辖市、三十一个省和两个特别行政区组成。

学校由教师、职工和学生组成。

学习小组由名称、人数、组长、成员和活动按排组成。

内部类和外部类 例 4.4

4.2.1 内部类特性

4.2.2 静态内部类内部类和外部类

public class Group1

{

int count; //外部类的成员变量

public class Student //声明内部类

{

String name; //内部类的成员变量

public void output() //内部类的成员方法

{

System.out.println(this.name +" ");

}

}

}

4.2.1 内部类特性

1在外部类中可直接使用内部类的类名,否则必须使用完整的类标识:外部类名,内部类名,例如:大陆,清华大学,台湾,清华大学

2 内部类是外部类的一个成员,成员变量、成员方法和 成员类 。内部类是抽象概念,使用时必须对象化。苹果的故事。 例 4.5

3 内部类和外部类各有自己的成员,例 4.6

4 抽象内部类,例 4.7

5 内部接口,例 4.8

public class Group2

{ private int count; //外部类的私有成员变量

public class Student //声明内部类

{ String name;

public Student(String n1)

{ name = n1;

count++; //存取其外部类的成员变量

}

public void output()

{ System.out.print(this.name +" ");

}

}

Student s1 = new Student("李明 "); //建立内部类对象 s1

public void output() //外部类的实例成员方法

{ s1.output(); //通过 s1调用内部类的成员方法

System.out.println(" count = "+this.count);

}

public static void main (String args[])

{ Group2 g2 = new Group2();

g2.output();

}

}

public class Group3

{ private static int count; //静态变量 Group3.count统计班级数量

private String name; //实例变量 Group3.name 表示班级名称

public class Student

{ private int count; //实例变量 Student.count表示学号

private String name; //实例变量 Student.name表示学生姓名

public void output(int count)

{ count++; //存取方法的参数,局部变量

this.count++; //通过对象存取 Student.count

Group3.count++; //通过类名存取 Group3.count

Group3.this.count++; //通过对象名存取 Group3.count

System.out.println(count+" "+this.count+" "+

Group3.count+" "+Group3.this.count++);

}

}

public Student aStu() //返回内部类 Student的一个对象

{ return new Student();

}

public static void main (String args[])

{ Group3 g3 = new Group3();

g3.count=10; //Group3.count

Group3.Student s1 = g3.aStu(); //在外部创建内部类的对象

//完整的内部类标识 Group3.Student

s1.output(5);

}

} 6 1 12 12

public class Group4

{ public abstract class Student_abstract //抽象内部类

{ int count;

String name;

public abstract void output(); //抽象方法

}

public class Student extends Student_abstract //继承抽象内部类

{ public Student(String n1)

{ name = n1;

count++; //Student.count

}

public void output() //实现抽象方法

{ System.out.println(this.name +" count="+this.count);

}

}

public Group4()

{ Student s1 = new Student("A");

s1.output();

Student s2 = new Student("B");

s2.output();

}

public static void main (String args[])

{ Group4 g4 = new Group4();

}

}

A count=1

B count=1

public class Group5

{ public interface Student_info //内部接口

{ public void output();

}

public class Student implements Student_info //内部类实现内部接口

{ int count;

String name;

public Student(String n1)

{ name = n1;

count++;

}

public void output() //实现接口方法

{ System.out.println(this.name +" count="+this.count);

}

}

public Group5(String name1[])

{ Student s1;

int i=0;

while (i

{ s1 = new Student(name1[i]);

s1.output();

i++;

}

}

public static void main (String args[])

{ Group5 g5;

if (args.length>0)

g5 = new Group5(args);

}

}

A count=1

B count=1

C count=1

Java Group5 A B C

4.2.2 静态内部类

静态内部类的特点:性格极端孤僻,封闭系统,不引用外部类或其它内部类中的成员。

顶层类:像皇上,孤家寡人,独立性强,

但权力并不像皇上那么大。

例 4.9

public class Group6

{ public static class Student //定义静态公用内部类

{ static int count; //静态内部类中的静态变量

String name;

int number; //序号

public Student(String n1) //静态内部类的构造方法

{ name = n1;

count++;

number = count; //序号自动增加

}

public void output()

{ System.out.println(this.name +" number="+this.number);

}

}

public static void main (String args[])

{ Group6.Student s1 = new Group6.Student("A");

s1.output();

Group6.Student s2 = new Group6.Student("B");

s2.output();

}

}

A number=1

B number=2

4.3 包

包与文件夹

4.3.1 Java的 API介绍

API:应用程序编程接口,类库

Java.lang 语言包

Java.util 实用包

Java.awt 抽象窗口工具包

Java.text 文本包

Java.io 输入输出流的文件包

Java.applet Applet应用程序包

Java.net 网络功能包

4.3.2 引用 Java定义的包

4.3.3 自定义包

4.3.1 Java的 API介绍

Java.lang 语言包 例 4.10

1,对象类 Object

2,数据类型包装类

3,字符串类 String & StringBuffer

4,数学类 Math

5,系统和运行时类 System & Runtime

6,类操作类 Class

7,错误和异常处理类 Throwable

8,线程类 Thread

9,过程类 Process

4.3.2 引用 Java定义的包

1,导入包

2,Java包的路径:例 4.11

4.3.3 自定义包

1,包的定义格式 默认包

2,设置包的路径:例 4.12

3,多个类属于同一个包

4,创建包等级

5,Java程序结构 与类的封装

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值