Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30a9c2f

Browse files
authored
Add files via upload
1 parent bd097cf commit 30a9c2f

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mayaha.dp.intepreter;
2+
3+
public class AndExpression implements Expression {
4+
5+
private Expression expr1 = null;
6+
private Expression expr2 = null;
7+
8+
public AndExpression(Expression expr1, Expression expr2) {
9+
this.expr1 = expr1;
10+
this.expr2 = expr2;
11+
}
12+
13+
@Override
14+
public boolean interpret(String context) {
15+
return expr1.interpret(context) && expr2.interpret(context);
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.mayaha.dp.intepreter;
2+
3+
public interface Expression {
4+
public boolean interpret(String context);
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mayaha.dp.intepreter;
2+
//创建规则和演示表达式的解析
3+
public class Main {
4+
//规则:Robert 和 John 是男性
5+
public static Expression getMaleExpression(){
6+
Expression robert = new TerminalExpression("Robert");
7+
Expression john = new TerminalExpression("John");
8+
return new OrExpression(robert, john);
9+
}
10+
11+
//规则:Julie 是一个已婚的女性
12+
public static Expression getMarriedWomanExpression(){
13+
Expression julie = new TerminalExpression("Julie");
14+
Expression married = new TerminalExpression("Married");
15+
return new AndExpression(julie, married);
16+
}
17+
18+
public static void main(String[] args) {
19+
Expression isMale = getMaleExpression();
20+
Expression isMarriedWoman = getMarriedWomanExpression();
21+
22+
System.out.println("John is male? " + isMale.interpret("John"));
23+
System.out.println("Julie is a married women? "
24+
+ isMarriedWoman.interpret("Married Julie"));
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mayaha.dp.intepreter;
2+
3+
public class OrExpression implements Expression {
4+
5+
private Expression expr1 = null;
6+
private Expression expr2 = null;
7+
8+
public OrExpression(Expression expr1, Expression expr2) {
9+
this.expr1 = expr1;
10+
this.expr2 = expr2;
11+
}
12+
13+
@Override
14+
public boolean interpret(String context) {
15+
return expr1.interpret(context) || expr2.interpret(context);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mayaha.dp.intepreter;
2+
3+
public class TerminalExpression implements Expression {
4+
5+
private String data;
6+
7+
public TerminalExpression(String data){
8+
this.data = data;
9+
}
10+
11+
@Override
12+
public boolean interpret(String context) {
13+
if(context.contains(data)){
14+
return true;
15+
}
16+
return false;
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /