//variables and constants
var currentVersionCode = 1 //变量当前的版本号,类型Int可以根据值推断出来
var currentVersionName : String = "1.0" //显式标明类型
val APPNAME = "droidyue.com" //常量APPNAME 类型(String)可以根据值推断出来
//methods
fun main(args: Array<String>) {
println(args)
}
// class
class MainActivity : AppCompatActivity() {
}
// data class 自动生成getter,setting,hashcode和equals等方法
data class Book(var name: String, val price: Float, var author: String)
//支持默认参数值,减少方法重载
fun Context.showToast(message: String, duration:Int = Toast.LENGTH_LONG) {
Toast.makeText(this, message, duration).show()
}
public void test(String string) {
if (string != null) {
char[] chars = string.toCharArray();
if (chars.length > 10) {
System.out.println(((Character)chars[10]).hashCode());
}
}
}
fun testNullSafeOperator(string: String?) {
System.out.println(string?.toCharArray()?.getOrNull(10)?.hashCode())
}
testNullSafeOperator(null)
testNullSafeOperator("12345678901")
testNullSafeOperator("123")
//result
null
49
null
public static void longToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
fun Context.longToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
applicationContext.longToast("hello world")
findViewById(R.id.content).setOnClickListener {
Log.d("MainActivity", "$it was clicked")
}
fun SharedPreferences.editor(f: (SharedPreferences.Editor) -> Unit) {
val editor = edit()
f(editor)
editor.apply()
}
//实际调用
PreferenceManager.getDefaultSharedPreferences(this).editor {
it.putBoolean("installed", true)
}
fun parse(url: String): Unit {
Jsoup.parse(URL(url), PARSE_URL_TIMEOUT).getElementsByClass("table table-sm")
.first().children()
.filter { "tbody".equals(it.tagName().toLowerCase()) }
.flatMap(Element::children).forEach {
trElement ->
ProxyItem().apply {
trElement.children().forEachIndexed { index, element ->
when (index) {
0 -> {
host = element.text().split(":")[0]
port = element.text().split(":")[1].toInt()
}
1 -> protocol = element.text()
5 -> country = element.text()
}
}
}.let(::println)
}
}
val book = Book("Thinking In Java", 59.0f, "Unknown")
val extraValue = "extra"
Log.d("MainActivity", "book.name = ${book.name}; book.price=${book.price};extraValue=$extraValue")
fun handle(): Unit {
window.onload = {
document.getElementsByClassName("table table-sm").asList().first()
.children.asList().filter { "TBODY".equals(it.tagName.toUpperCase()) }
.flatMap { it.children.asList() }.forEach {
var proxyItem = ProxyItem()
it.children.asList().forEachIndexed { index, element ->
when (index) {
0 -> {
proxyItem.host = element.trimedTextContent()?.split(":")?.get(0) ?: ""
proxyItem.port = element.trimedTextContent()?.split(":")?.get(1)?.trim()?.toInt() ?: -1
}
1 -> proxyItem.protocol = element.trimedTextContent() ?: ""
5 -> proxyItem.country = element.trimedTextContent() ?: ""
}
}.run {
console.info("proxyItem $proxyItem")
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有