【发布时间】:2017-11-26 16:17:10
【问题描述】:
在 Java 中,我可以使用 [class-name].[method] 来更改类中变量的值,例如:
public class Main {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
test.teste();
test.printlnvar();
}
}
class test{
public static int a = 0;
public static int b = 0;
public static void teste(){
a = 9;
b = 12;
}
public static void printlnvar(){
System.out.println("the value of A: " + a);
System.out.println("the value of B: " + b);
}
}
但是,我如何在 Kotlin 中做到这一点?我尝试了,但在下面的代码中,变量 IntColumn 和 IntRow 的结果始终为 0:
public class drawTriangle{
public var IntColumn:Int = 0;
public var IntRow:Int = 0;
fun drawTriangle(){
this.inputRowandColumn();
this.Printvalue();
}
fun inputRowandColumn(){
IntColumn = 12;
IntRow = 3;
}
fun Printvalue(){
println("the value of rows is: ${IntRow}");
println("the value of column is: ${IntColumn}");
}
}
fun main(args: Array<String>){
drawTriangle().inputRowandColumn();
drawTriangle().Printvalue();
}
【问题讨论】: