java中为什么接口中只能定义常量?如果属性是可变的怎么办?

yyxgs 2017-08-18 02:07:12
比如,我要定义一个牌类的接口。
牌有正反面,有字符,关键是这个字符是可变的,可以是[10],[J],[Q],[K],[A]....等等
每张牌都有字符, 这可以抽象出来吧?关键是这属性又是可变的。
请问该怎么设计呢?
...全文
766 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 13 楼 qq_27762917 的回复:
[quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq_27762917 的回复:] public声明的类要和文件名一致,楼主看看是不是不一致
这就奇怪了,为什么在另外一个类中不合法呢?[/quote] 我刚写了一个扑克类你可以参考下 public class Test { public static void main(String[] args) { Card card = new Card(true,"A",1); System.out.println(card.toString());// 黑桃A } } class Card { private boolean status;// 正反面(true:正面,false:反面) private String score;// 分值 private int type;// 花色 private static final Map<Integer, String> types = new HashMap<Integer, String>(); static { types.put(1,"黑桃"); types.put(2,"红桃"); types.put(3,"梅花"); types.put(4,"方块"); } private static final List<String> SCORES = Arrays.asList(new String[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }); public Card(boolean status, String score, int type) { if (!SCORES.contains(score) || type > 4 || type < 1) throw new IllegalArgumentException("参数错误"); this.status = status; this.score = score; this.type = type; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String toString() { return types.get(this.type) + this.score; } }[/quote] 谢谢!学习了!
Freefish1994 2017-08-18
  • 打赏
  • 举报
回复
引用 12 楼 yyxgs 的回复:
[quote=引用 9 楼 qq_27762917 的回复:] public声明的类要和文件名一致,楼主看看是不是不一致
这就奇怪了,为什么在另外一个类中不合法呢?[/quote] 我刚写了一个扑克类你可以参考下 public class Test { public static void main(String[] args) { Card card = new Card(true,"A",1); System.out.println(card.toString());// 黑桃A } } class Card { private boolean status;// 正反面(true:正面,false:反面) private String score;// 分值 private int type;// 花色 private static final Map<Integer, String> types = new HashMap<Integer, String>(); static { types.put(1,"黑桃"); types.put(2,"红桃"); types.put(3,"梅花"); types.put(4,"方块"); } private static final List<String> SCORES = Arrays.asList(new String[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }); public Card(boolean status, String score, int type) { if (!SCORES.contains(score) || type > 4 || type < 1) throw new IllegalArgumentException("参数错误"); this.status = status; this.score = score; this.type = type; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String toString() { return types.get(this.type) + this.score; } }
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 9 楼 qq_27762917 的回复:
public声明的类要和文件名一致,楼主看看是不是不一致
这就奇怪了,为什么在另外一个类中不合法呢?
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 9 楼 qq_27762917 的回复:
public声明的类要和文件名一致,楼主看看是不是不一致
我把代码直接复制到主类文件中,合法了。
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 9 楼 qq_27762917 的回复:
public声明的类要和文件名一致,楼主看看是不是不一致
我这是新建的另外的一个类文件。
Freefish1994 2017-08-18
  • 打赏
  • 举报
回复
public声明的类要和文件名一致,楼主看看是不是不一致
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 5 楼 yyxgs 的回复:
[quote=引用 2 楼 yuxiangaaaaa 的回复:]
[quote=引用 1 楼 yyxgs 的回复:]
class Card {
private boolean status; /*正反面状态*/
private String words; /*字符*/

public Card(String _words) {
status = true;
words = _words;
}

public String getWords() {
return words;
}
}


如果这样写,那么一组牌就要用继承,我看到一篇文章说初学者少用继承,多用接口。


根据事实呀,少用不是不能用,接口不能满足你的要求,就用抽象类呗[/quote]

嗯,我试试抽象类。[/quote]




为什么这样写IDE报错?
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 6 楼 qq_27762917 的回复:
[quote=引用 4 楼 yyxgs 的回复:] [quote=引用 3 楼 qq_27762917 的回复:] 接口中是不允许定义变量的只允许定义常量,你就写一个父类里面有状态、花色还有数字啥的,其它类都继承这个类不得了。。。
别人说多用接口,少用继承。[/quote] 接口主要是用来定义方法的,你这个是类,里面有各种字段,肯定要有一个父类用来继承的,你顶多是在接口里定义一个比如“发牌”、“洗牌”这样的无论什么牌都需要的方法[/quote] 我的想法是先定义一张牌类,只有一张牌所属的数据,然后另外定义一个牌类,由一组一张牌类的对象组成。 那如果定义发牌、洗牌的接口,应该由哪个类实现呢?
Freefish1994 2017-08-18
  • 打赏
  • 举报
回复
引用 4 楼 yyxgs 的回复:
[quote=引用 3 楼 qq_27762917 的回复:] 接口中是不允许定义变量的只允许定义常量,你就写一个父类里面有状态、花色还有数字啥的,其它类都继承这个类不得了。。。
别人说多用接口,少用继承。[/quote] 接口主要是用来定义方法的,你这个是类,里面有各种字段,肯定要有一个父类用来继承的,你顶多是在接口里定义一个比如“发牌”、“洗牌”这样的无论什么牌都需要的方法
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 2 楼 yuxiangaaaaa 的回复:
[quote=引用 1 楼 yyxgs 的回复:]
class Card {
	private boolean status;	/*正反面状态*/
	private String words;	/*字符*/
	
	public Card(String _words) {
		status = true;
		words = _words;
	}
	
	public String getWords() {
		return words;
	}
}
如果这样写,那么一组牌就要用继承,我看到一篇文章说初学者少用继承,多用接口。
根据事实呀,少用不是不能用,接口不能满足你的要求,就用抽象类呗[/quote] 嗯,我试试抽象类。
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
引用 3 楼 qq_27762917 的回复:
接口中是不允许定义变量的只允许定义常量,你就写一个父类里面有状态、花色还有数字啥的,其它类都继承这个类不得了。。。
别人说多用接口,少用继承。
自由自在_Yu 2017-08-18
  • 打赏
  • 举报
回复
引用 1 楼 yyxgs 的回复:
class Card {
	private boolean status;	/*正反面状态*/
	private String words;	/*字符*/
	
	public Card(String _words) {
		status = true;
		words = _words;
	}
	
	public String getWords() {
		return words;
	}
}
如果这样写,那么一组牌就要用继承,我看到一篇文章说初学者少用继承,多用接口。
根据事实呀,少用不是不能用,接口不能满足你的要求,就用抽象类呗
Freefish1994 2017-08-18
  • 打赏
  • 举报
回复
接口中是不允许定义变量的只允许定义常量,你就写一个父类里面有状态、花色还有数字啥的,其它类都继承这个类不得了。。。
yyxgs 2017-08-18
  • 打赏
  • 举报
回复
class Card {
	private boolean status;	/*正反面状态*/
	private String words;	/*字符*/
	
	public Card(String _words) {
		status = true;
		words = _words;
	}
	
	public String getWords() {
		return words;
	}
}
如果这样写,那么一组牌就要用继承,我看到一篇文章说初学者少用继承,多用接口。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧