java-kotlin实战——生成一副扑克牌,并从中随机抽取两张牌比较大小
一、逻辑构思
1、需要哪些类参与
-扑克牌点数
点数“ 2 3 4 5 6 7 8 9 10 j Q K A”
点数大小权重(用以比较)
-扑克牌花色
花色“ ♠ ♥ ♣ ♦”
花色权重(用以比较)
-扑克牌:
点数
花色
-一副扑克牌:
包括所有的扑克牌
-扑克牌管理器:
获得一副牌,并拥有进行比较的方法
2、每个类拥有哪些成员方法
-扑克牌点数
getter,setter
-扑克牌花色
getter,setter
-扑克牌:
getter,setter
boolean compare(用以比较大小)
-扑克牌管理器:
constructor:用以获得一副牌
getPoker:用以获得随机一张牌并且在一副扑克牌中删去此牌
构建UML类图
二、准备工作
主要知识点准备:java与kotlin的构造方法,ArrayList对象数组的使用
1、java与kotlin的构造方法(我们以扑克牌点数构造方法为例)
java语言的构造方法
public ClassName ( String tran1,String tran2){
this.Mumber1 = tran1;
this.Mumber2 = tran2;
}
kotlin语言的构造方法
在定义的同时给予赋值
class ClassName(val Member1: String,
val Member2 : Int
)
2、ArrayList对象数组的使用
Java语言
ArrayList<数组元素类型> name = new ArrayList<>();
kotlin语言
val Pokers:ArrayList<Poker> = arrayListOf()
三、代码实现
此处将java与kotlin代码分模块一起放置,以增强对比性
*首先构造扑克牌点数类,保障安全性将number与tag私有化,通过getter与setter与外界连接
setter:给成员变量赋值 getter:获取成员变量的值
同时为提高代码可读性,将getter与setter写在最后
//java
public class PokerNumber {
String number;
int tag;
public PokerNumber(String number, int tag) {
this. number = number;
this.tag = tag;
}
}
//kotlin
class PokerNumber(val number: String,
val tag : Int
)
*扑克牌花色类同上
public class PokerSuit {
String suit;
int tag;
public PokerSuit(String suit, int tag) {
this.suit = suit;
this.tag = tag;
}
}
//kotlin
class PokerSuit(val suit:String,
val tag : Int
)
*构造扑克牌类,拥有以上两个类成员变量,获得他们的值,
我们最后是以两张扑克牌为基础进行比较,此时在扑克牌Poker类中构造compare方法用以比较大小最为妥当
public class Poker {
PokerNumber NumberObj;
PokerSuit SuitObj;
public Poker(PokerNumber numberObj, PokerSuit suitObj) {
this. NumberObj = numberObj;
this. SuitObj = suitObj;
}
public boolean Compare(Poker other) {
boolean result = true;
//此处我们默认result值为true,仅考虑不符合扑克牌1大于扑克牌2的情况
if (this.NumberObj.tag == other.NumberObj.tag) {
if (this.SuitObj.tag < other.SuitObj.tag) {
result = false;
}
} if (this.NumberObj.tag < other.NumberObj.tag) {
result = false;
}
return result;
}
}
//kotlin
class Poker(
val numerObj:PokerNumber,
val suitObj: PokerSuit
){
//当前这张牌和other牌比较
fun compareTo(other:Poker): Boolean =if (this.numerObj.tag == other.numerObj.tag)
{this.suitObj.tag > other.suitObj.tag}
else{this.numerObj.tag > other.numerObj.tag}
}
*构造扑克牌管理器Poker Manager,(通过无参构造方法)获得一副牌,并构建一个getpoker的方法,从这副牌中随机取出一张牌并在原有的牌中删去此牌。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class PokerManager {
ArrayList<Poker> Pokers = new ArrayList<>();
String[] Numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
String[] Suit = {"♦", "♣", "♥", "♠"};
public PokerManager() {
for (int i = 0; i < Numbers.length; i++) {
for (int j = 0; j < Suit.length; j++) {
PokerNumber pokerNumber = new PokerNumber(Numbers[i],i);
PokerSuit pokerSuit = new PokerSuit(Suit[j],j );
Poker poker = new Poker(pokerNumber,pokerSuit);
Pokers.add(poker);
}
}
Collections.shuffle(Pokers); //用来打乱原有的顺序
}
public Poker getPoker() { //随机获取一张牌
Random random =new Random();
int index = random.nextInt(Pokers.size());
Poker poker = Pokers.get(index);
Pokers.remove(index); //在扑克牌中删去该牌
return poker;
}
}
//kotlin
import kotlin.random.Random
class PokerManager{
//先提供一个容器,存储所有的扑克
val Pokers:ArrayList<Poker> = arrayListOf()
//点数的数组
val numbers = arrayOf("2","3","4","5","6","7","8","9","10","J","Q","K","A")
//花色的数组
val suits = arrayOf("♦","♣","♥","♠")
init {
for ((i,number)in numbers.withIndex()){
for ((j,suit)in suits.withIndex()){
val numberObj = PokerNumber(number,i)
var suitObj= PokerSuit(suit,j)
//创建扑克对象
val Poker = Poker(numberObj,suitObj)
//添加到数组中
Pokers.add(Poker)
}
}
}
fun getPoker():Poker{
val index = Random.nextInt(Pokers.size)
val poker = Pokers[index]
Pokers.removeAt(index)
return poker
}
}
*最后构建运行类,测试代码
public class Test {
public static void main(String[] args) {
PokerManager pokerManager =new PokerManager();
Poker poker1 =pokerManager.getPoker(); //随机获取两张牌
Poker poker2 = pokerManager.getPoker();
boolean result = poker1.Compare(poker2);
if (result){
System.out.println(poker1.NumberObj.number+poker1.SuitObj.suit+">"+poker2.NumberObj.number+poker2.SuitObj.suit);
}else {
System.out.println(poker1.NumberObj.number+poker1.SuitObj.suit+"<"+poker2.NumberObj.number+poker2.SuitObj.suit);
}
}
}
//kotlin
fun main() {
var manager = PokerManager()
val poker1 = manager.getPoker()
val poker2 = manager.getPoker()
if (poker1.compareTo(poker2)){
println("${poker1.numerObj.number}${poker1.suitObj.suit} > ${poker2.numerObj.number}${poker2.suitObj.suit} ")
}else{
println("${poker1.numerObj.number}${poker1.suitObj.suit} < ${poker2.numerObj.number}${poker2.suitObj.suit} ")
}
}
*最后运行程序,返回结果
总结
在此次实战中,我们通过实际代码数量,可读性多方面对java与kotlin进行比较
Java与kotlin相比,在代码数量上kotlin能够以更短的代码实现更多的功能,相较于构
造函数上kotlin在定义成员变量的同时就可为其赋值,使得代码更加简洁Kotlin中语句不需要以;结束,Java以;结束;
Kotlin中数据类型是后置的;
Kotlin方法使用fun关键字定义;
- Kotlin中使用var,val声明变量及属性,可以进行类型推断,编译器可以根据赋值自动推断其类型为String,而Java声明变量必须先指定其类型;