-
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
1614010405乔丽萍 #33
Comments
末尾只需要 ``` 不需要java |
第二次作业简单工厂模式工厂模式就是建立一个工厂类。到底要实例化谁,将来会不会增加实例化对象,考虑用一个单独的类来做这个创造实例的过程,这就是工厂。 Operation运算类public class Operation {
private double _numberA = 0;
private double _numberB = 0;
public double get_numberA(){
return _numberA;
}
public void set_numberA(double _numberA) {
this._numberA = _numberA;
}
public double get_numberB() {
return _numberB;
}
public void set_numberB(double _numberB) {
this._numberB = _numberB;
}
public double GetResult() {
double result = 0;
return result;
}
} 加减乘除类//创建不同的类实现低耦合
public class OperationAdd extends Operation{
public double GetResult(){
double result = 0;
result = get_numberA() +get_numberB();
return result;
}
}
public class OperationSub extends Operation {
public double GetResult(){
double result = 0;
result =get_numberA() - get_numberB();
return result;
}
}
public class OperationMul extends Operation {
public double GetResult(){
double result = 0;
result = get_numberA() *get_numberB();
return result;
}
}
public class OperationDiv extends Operation {
public double GetResult() {
double result = 0;
if(get_numberB()==0){
try
{
throw new Exception("除数不能为零。");
}catch (Exception e){
System.out.println(e);
}
}
result = get_numberA()/get_numberB();
return result;
}
} 简单运算工厂类//只需要输入运算符号,工厂就实例化出合适的对象,通过多态,返回父类的方法实现了计算器的结果
public class OperationFactory {
public static Operation createOperate(String operate)
{
Operation oper = null;
switch (operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
} 客户端代码public class text {
public static void main(String[] args){
Operation oper;
oper = OperationFactory.createOperate("+");
oper.set_numberA(1) ;
oper.set_numberB(2);
double result = oper.GetResult();
System.out.println(result);
}
} Java 8 之后有了接口为什么还要用抽象类?
|
第三次作业import java.util.*;
import java.util.stream.Collectors;
public class text {
public static void main(String[] args){
int i;
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
a.add("I");
a.add("am");
a.add("coming");
//for循环输出
System.out.println("for循环输出");
for( i = 0;i < a.size();i ++)
{
System.out.println(a.get(i));
}
//while循环输出
System.out.println("\nwhile循环输出");
i = 0;
while(i<a.size()){
System.out.println(a.get(i));
i++;
}
//foreach循环输出
System.out.println("\nforeach循环输出");
for (String x: a
) {
System.out.println(x);
}
//stream流输出
System.out.println("\nstream流输出");
List<String> s = a.stream().collect(Collectors.toList());
System.out.println(s);
}
} 输出结果for循环输出 while循环输出 foreach循环输出 stream流输出 |
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: