类成员 | 顶层声明 | ||
---|---|---|---|
public | 默认可见性 | 所有地方可见 | 所有地方可见 |
private | 类中可见 | 文件中可见 | |
protected | 子类中可见 | ||
internal | 模块中可见 | 模块中可见 |
fun test(vararg tags: String) {
println(tags[0])
var list = listOf(*tags)
println(list)
}
区别 | |
---|---|
var | private |
val | private final |
enum class TimeTypeEnum(val value: String) {
LATESTWEEK("1"),
LATESTMONTH("2"),
LATESTYEAR("3")
}
for(key in 0 until normal_praise){
LogUtil.d("=====key===${key}")
}
var flag:Any="ddd"
Object flag = "ddd";
默认java调用kt的静态属性是需要调用getXX()的
Cat.Companion.getAge() //访问Cat.kt 文件的age属性
用 const 修饰后 相当于以 public staic final 的属性直接暴露给java
Cat.age
val str:String?=null
println(str.toString()) //打印null
println(str!!.toString()) //会报错,此时断言str不为null
interface CallBack {
fun callBack(str: String)
fun getTest(){
println("调用了接口里的方法")
}
}
class Cat {
companion object {
var name:String="hcy"
fun getName(){
println(name)
}
}
}
object Dog {
var name:String?=null
val age:Int=10
fun play(){
println("playing!!!!")
}
}
public final class Dog {
@Nullable
private static String name;
private static final int age = 10;
public static final Dog INSTANCE;
@Nullable
public final String getName() {
return name;
}
public final void setName(@Nullable String var1) {
name = var1;
}
public final int getAge() {
return age;
}
public final void play() {
String var1 = "playing!!!!";
boolean var2 = false;
System.out.println(var1);
}
private Dog() {
}
static {
Dog var0 = new Dog();
INSTANCE = var0;
age = 10;
}
}
v is String
v instanceof String
//原始字符串
fun func1(){
var str1="hcy\n"
val str="""hcy\n"""
for (c in str){
println(str.length)
}
}
内部没有转义
fun strTest(str:String):Unit{
for (c in str){
println(c)
}
}
fun funcIf(a: Int, b: Int) {
var max = if (a < b) {
a
} else {
b
}
println("max==${max}")
}
fun funcWhen(tag:Any):Unit{
when(tag){
1-> println("AAAA")
2-> println("BBBB")
3-> println("CCCC")
4,5->{
println("DDDD=or=EEEE")
}
in 6..8->{
println("在[6,8]区间里")
}
is Int->{
}
else -> println("其他")
}
}
kt 不支持三目表达式
var arr=Array(5){i->i}
arr.forEach {
println(it)//0,1,2,3,4
}
fun funcFor2():Unit{
var arr= arrayOf("AAA","BBB",333,444,555);
for (index in arr.indices){
println("index==${index},value==${arr[index]}")
}
}
fun funcFor():Unit{
var arr= arrayOf("A",2,3)
for ((i,v)in arr.withIndex()){
if (v is String){
println("String类型:index==${i},value==${v}")
}else{
println("index==${i},value==${v}")
}
}
}
for ((key,value) in params){
Log.d("主app参数::","key==${key},value==${value}")
}
val filterList =
data.filter { !(it.class_guid.equals(currentClass?.class_guid) && it.class_name.equals(currentClass?.class_name)) }
var total = 10 * 60 * 1000
for (i in total downTo 0 step 1000) {
Thread.sleep(1000)
println(i/1000)
}
var age: Int = 0
set(value) {
println("${name}age setter")
field = value
}
var address: String = ""
private set
interface Person {
var name: String
var age: Int
}
class OldStu(override var name: String, override var age: Int) : Person {
}
class Student(var per: OldStu, var sex: String) : Person by per {
fun getInfo() {
println("${per.age}==${per.name}===$sex")
}
}
startActivity(Intent(MainActivity@this,LoginActivity::class.java))
tv_refresh?.setOnClickListener {
present.getStudentContacts(
SPUtils.getInstance().getString(TeacherConstants.YID),
currentClassGuid,
"",
getThisActivity()
)
}
fun getTest(num:Int)=when(num){
1->{
"1"
}
else->{
"ddd"
}
}