-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1607064207--靳璐 #18
Labels
Comments
good |
Java第二次培训Java 8 之后有了接口为什么还要用抽象类?答:(1)在 Java 8 的接口中,即使有了 default method ,也暂时无法完全取代抽象类。 (2)接口中没有 this ,没有构造函数,不能拥有实例变量或者实例方法,无法保存状态,而抽象方法可以。 (3)接口不能拥有非静态成员变量。Java 8 中接口只能提供静态常量、公有方法声明、公有默认方法和类方法;而 Java 9 新特性中允许接口中拥有私有方法。默认方法最初的设计目的是让已经存在的接口可以演化——添加新方法时不需要原本已经实现该接口的类做任何改变(甚至不需要重新编译)就可以直接使用新版本的接口,但是这已经在很大程度上覆盖了抽象类的特性。未来的 Java 版本的接口可能会有更强大的功能,或许能更大程度的替代原本需要抽象类的场景。 设计模式之工厂方法模式产品接口及产品实现: /**
*产品接口:定义产品实现的规范,所有的产品实现都必须遵循产品接口定义的规范,
* 也可以用抽象类来代替,注意不要违背里氏替换原则
*/
public interface IProduct {
void create();
void show();
}
//产品实现1
public class Product1 implements IProduct {
@Override
public void create() {
System.out.println("Create Product1");
}
@Override
public void show() {
System.out.println("Show Product1");
}
}
//产品实现2
public class Product2 implements IProduct{
@Override
public void create() {
System.out.println("Create Product2");
}
@Override
public void show() {
System.out.println("Show Product2");
}
} 工厂接口及工厂实现: //工厂接口:工厂方法模式的核心
public interface IFactory {
public IProduct createProduct();
}
/**
*工厂实现:决定如何实例化产品,是实现扩展的途径,需要有多少种产品,
* 就有多少个具体的工厂实现
*/
//创建product1对象的工厂实现
public class FactoryMethod1 implements IFactory {
@Override
public IProduct createProduct() {
return new Product1();
}
//创建product2对象的工厂实现
public class FactoryMethod2 implements IFactory {
@Override
public IProduct createProduct() {
return new Product2();
}
} 客户类: //客户类
public class Client {
public static void main(String[] args) {
//创建product1类型的具体产品
IFactory iFactory1 = new FactoryMethod1();
IProduct product1 = iFactory1.createProduct();
product1.create();
product1.show();
//创建product2类型的具体产品
IFactory iFactory2 = new FactoryMethod2();
IProduct product2 = iFactory2.createProduct();
product2.create();
product2.show();
}
} 运行结果:
|
遍历集合的几种方式fori遍历public class Each {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(27,"kris"));
list.add(new Student(19,"jin"));
list.add(new Student(22,"hao"));
list.add(new Student(18,"lin"));
list.add(new Student(20,"qin"));
list.add(new Student(21,"zhi"));
list.add(new Student(19,"yu"));
list.add(new Student(21,"zhong"));
list.add(new Student(44,"ma"));
list.add(new Student(49,"fa"));
for (int i = 0; i < list.size(); i++) {
System.out.println("name is:" + list.get(i).getName() + "\tage is:" + list.get(i).getAge());
}
}
} foreach遍历for (Student s:
list) {
System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge());
} Lambda表达式遍历集合list.forEach(s-> System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge())); 集合操作符stream遍历集合System.out.println("(1)Lambda表达式");
list.stream().forEach(s-> System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge()));
System.out.println("(2)accept------");
list.stream().forEach(new Consumer<Student>() {
@Override
public void accept(Student student) {
System.out.println("name is:" + student.getName() + "\tage is:" + student.getAge());
}
}); 迭代器遍历集合Iterator<Student> iterator = list.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge());
} 遍历结果:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
策略模式&排序算法
基础类
插入排序
选择排序
快速排序
策略类
测试类
测试结果
The text was updated successfully, but these errors were encountered: