//类是单继承模式,而接口允许多继承
public interface ITest extends ITree,ISwim{
}
如果一个类实现了某一个接口,则该类必须重写接口中的所有方法; (不对,因为抽象类可以不重写接口中的方法 )
//接口中的方法都是public abstract的,在实现类中不能缩小接口中方法的可见性
public interface ISwim {
public void swim(Pool pool);
}
//在一个类文件中,可以在Pet Class外面,再定义其它的修饰符为默认关键字的Class-----目的就是Test类为包内可见,提高了代码的封装性
public abstract class Pet implements ISwim{
public String name; //名字
}
class Test{
private String name;
}
//注意:Class的修饰符,外部类可以使用public 和 默认关键字,不能使用private和protected
内部类可以使用public 、 默认关键字、private和protected
//在一个类的内部,可以定义其它类和接口
public abstract class Pet implements ISwim{
public String name; //名字
protected double age; //年龄
protected String color; //颜色
class Test{
private String name;
}
public interface IDiveWater {
public void diving(Pool pool);
}
}
//内部类的主要目的,是不想让外面的对象访问(通过间接方式可以访问),因此它是封装的一种表现
内部类的最大特点就是可以方便的访问它所在外部类的成员变量 注意:static类型的内部不能访问所在类的成员变量(static class Test{})
//匿名类
public static void main(String[] args) {
TestGame game = new TestGame();
ITree cat = new Cat("mimi");
game.testTree(cat);
game.testTree(new ITree(){
@Override
public void climTree(Tree tree) {
System.out.println("xxxx is climing tree");
}
} );
}
接口: Iterator 、 Collection 、List 、Set 、 Queue 、 Map
实现类: ArrayList 、 LinkedList 、HashSet 、LinkedBlockingQueue、HashMap
参考类: Vector 、 Stack 、 HashTable
静态数组的特点:Student[] stus = new Student[50];
内存空间提前分配好,然后在里面存储数据;但是空间的大小不能改变;可能会出现内存利用率低或存储空间不够的现象;
集合: 一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的
java.util 接口 Collection<E>
boolean add(E e)
确保此 collection 包含指定的元素(可选操作
remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
int size()
返回此 collection 中的元素数
//如何遍历 迭代器(只读访问集合的一种游标)
泛型: Collection<Student> stus; 表示集合中只能存放学生数据;
泛型是一种数据类型的约束条件;
告诉外面,特定的数据操作只能使用什么数据类型
java.util 接口 Iterator<E>
public interface ITest extends ITree,ISwim{
}
如果一个类实现了某一个接口,则该类必须重写接口中的所有方法; (不对,因为抽象类可以不重写接口中的方法 )
//接口中的方法都是public abstract的,在实现类中不能缩小接口中方法的可见性
public interface ISwim {
public void swim(Pool pool);
}
//在一个类文件中,可以在Pet Class外面,再定义其它的修饰符为默认关键字的Class-----目的就是Test类为包内可见,提高了代码的封装性
public abstract class Pet implements ISwim{
public String name; //名字
}
class Test{
private String name;
}
//注意:Class的修饰符,外部类可以使用public 和 默认关键字,不能使用private和protected
内部类可以使用public 、 默认关键字、private和protected
//在一个类的内部,可以定义其它类和接口
public abstract class Pet implements ISwim{
public String name; //名字
protected double age; //年龄
protected String color; //颜色
class Test{
private String name;
}
public interface IDiveWater {
public void diving(Pool pool);
}
}
//内部类的主要目的,是不想让外面的对象访问(通过间接方式可以访问),因此它是封装的一种表现
内部类的最大特点就是可以方便的访问它所在外部类的成员变量 注意:static类型的内部不能访问所在类的成员变量(static class Test{})
//匿名类
public static void main(String[] args) {
TestGame game = new TestGame();
ITree cat = new Cat("mimi");
game.testTree(cat);
game.testTree(new ITree(){
@Override
public void climTree(Tree tree) {
System.out.println("xxxx is climing tree");
}
} );
}
接口: Iterator 、 Collection 、List 、Set 、 Queue 、 Map
实现类: ArrayList 、 LinkedList 、HashSet 、LinkedBlockingQueue、HashMap
参考类: Vector 、 Stack 、 HashTable
静态数组的特点:Student[] stus = new Student[50];
内存空间提前分配好,然后在里面存储数据;但是空间的大小不能改变;可能会出现内存利用率低或存储空间不够的现象;
集合: 一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的
java.util 接口 Collection<E>
boolean add(E e)
确保此 collection 包含指定的元素(可选操作
remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
int size()
返回此 collection 中的元素数
//如何遍历 迭代器(只读访问集合的一种游标)
泛型: Collection<Student> stus; 表示集合中只能存放学生数据;
泛型是一种数据类型的约束条件;
告诉外面,特定的数据操作只能使用什么数据类型
java.util 接口 Iterator<E>