构造函数
Java
final File file = new File("file.txt");
Kotlin
val file = File("file.txt")
类
Java
public final class User {
}
Kotlin
class User
公开类
Java
public class User {
}
Kotlin
open class User
属性类
Java
final class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Kotlin
class User(val name: String)
有参数的构造函数
Java
final class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Kotlin
class User(var name: String)
构造函数中的可选参数
Java
final class User {
private String name;
private String lastName;
public User(String name) {
this(name, "");
}
public User(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
// And Getters & Setters
}
Kotlin
class User(var name: String, var lastName: String = "")
对类中参数赋值
Java
public class Document {
private String id = "00x";
public String getId() {
return id;
}
public void setId(String id) {
if(id != null && !id.isEmpty()) {
this.id = id;
}
}
}
Kotlin
class Document{
var id : String = "00x"
set(value) {
if(value.isNotEmpty()) field = value
}
}
抽象类
Java
public abstract class Document{
public abstract int calculateSize();
}
public class Photo extends Document{
@Override
public int calculateSize() {
}
}
Kotlin
abstract class Document {
abstract fun calculateSize(): Int
}
class Photo : Document() {
override fun calculateSize(): Int {
}
}
单例类
Java
public class Document {
private static final Document INSTANCE = new Document();
public static Document g服务器托管网etInstance(){
return INSTANCE;
}
}
Kotlin
object Document {
}
扩展类
Java
public class ByteArrayUtils {
public static String toHexString(byte[] data) {
}
}
final byte[] dummyData = new byte[10];
final String hexValue = ByteArrayUtils.toHexString(dummyData);
Kotlin
fun ByteArray.toHex() : String {
}
val dummyData = byteArrayOf()
val hexValue = dummyData.toHex()
内部类
Java
public class Documment {
class InnerClass {
}
}
Kotlin服务器托管网
class Document {
inner class InnerClass
}
嵌套类
Java
public class Documment {
public static class InnerClass {
}
}
Kotlin
class Document {
class InnerClass
}
接口
Java
public interface Printable {
void print();
}
public class Document implements Printable {
@Override
public void print() {
}
}
Kotlin
interface Printable{
fun print()
}
class Document : Printable{
override fun print() {
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: .NET开源免费的Windows快速文件搜索和应用程序启动器
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。 工具介绍 Flow Launcher 是一款方便实用的 Windows 文件搜索和应用程序启动器,能够…