一:继承与类与方法定义:
(1)继承用一个英文冒号: extends---> :
默认的类是不可以被继承的,只能继承声明为open或者abstract的类
open class Person (name: String , surname: String) {
}
(2)创建类用class:如 class A{},
还可以带参数,也可以写方法体:
open class Person (name: String , surname: String){
init {
//编写函数体
}
}
//class Person(name: String , surname: String)
(3)方法定义用fun:
//若没有方法类型(相当于java的void):默认返回Unit
//(1)定义一个没有参数,没有返回类型的方法
fun n1(){
}
//(2)定义一个有一个Int参数的无返回方法
fun add(x: Int ){
}
//(3)定义一个返回int类型的方法:两个参数
fun add(x: Int , y: Int) : Int{
return x + y; //分号可以省略的
}
可以定义没有返回类型的方法,如fun n1()
也可以定义有返回类型的方法,在方法后加冒号和返回类型,甚至返回值,如:fun n1() : Int{}
//(4)定义直接返回的方法:(3)的改写,与(3)一样
fun add2(x: Int , y: Int) : Int = x + y
还可以在参数中指定默认值,如:
//第三个参数指定了一个默认值,调用时可以不传
fun toast(context: Context, message: String, length: Int= Toast.LENGTH_SHORT){
Toast.makeText(context , message , length).show();
}
fun toast2(message: String , length: Int= Toast.LENGTH_SHORT){
Toast.makeText(this , message , length).show();
}
二,插入表达式'$’:
(1)当插入为简单类型时如字符串,可以直接加在$符号后面
(2)当插入为复杂类型时,person.name,则需要用花括号括起来再加在$符号后面
如下面所示:
//$代表插入表达式,复杂则用{}括起来,简单自己插入
fun SayHello(message: String) : String{
return "Your name is $message"
//return "66 is ${Person.name}"
}
Person是自己定义的bean类
三,val与var,以及各种基本类型
(1)val与var
val:只能赋值一次,不可以改变
---如:val i=1
i=2 //编译器会报错,val是不可以改变的
var:可以多次赋值,可以改变的
(2)基本类型:数字(Numbers),字符(Characters),布尔(Boolean),数组(Array),字符串(String)
数字:Byte,Short,Int,Long,Float,Double
几个基本类型默认:
val i = 1 //Int
val i2 = 0x0f //0x---16进制,,6进制Int
val d = 3.5 //Double
val f = 1.5f //Float类型
val l = 3L //Long类型
字符与字符串:
val str = "Hello"
val c = str[1]; //其实就是e
遍历字符串并打印:
val str = "Hello"
for(c in str){
print(c)
}
每个number类型和String支持如下的转换
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
Char类型支持
toInt(): Int
所有数据类型(不仅是基本数据类型)支持
toString(): String
如:
val a : Int=7 //定义一个Int变量a,值为7
val d : Double = a.toDouble() //把a转化为Double类型,赋给d
Log.d("MainActivity" , "a= " + a )
Log.d("MainActivity" , "d= " + d )
05-28 02:54:04.594 5560-5560/? D/MainActivity: a= 7
05-28 02:54:04.594 5560-5560/? D/MainActivity: d= 7.0
布局:定义一个RecyclerView:
Adapter类:
class ForecastListAdapter(val items: List) :
RecyclerView.Adapter() {
/**
* 初始化
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(TextView(parent.context))
}
/**
* 为holder绑定id
*/
override fun onBindViewHolder(holder: ViewHolder , position: Int) {
holder.textView.text = items[position]
}
/**
* 返回数目
*/
override fun getItemCount(): Int {
return items.size
}
/**
* 创建一个内部的ViewHolder类:定义一个TextView
*/
class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
}
主类:
class MainActivity : AppCompatActivity() {
//创建一个私有的不可变的List类型
private val items = listOf(
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30",
"Mon 5/28 - Sunny - 31/17",
"Tue 5/29 - Foggy - 21/9",
"Wed 5/30 - Cloudy - 19/11",
"Thurs 6/1 - Rainy - 27/25",
"Fri 6/2 - Foggy - 29/27",
"Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
"Sun 6/4 - Sunny - 32/30"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.recycler_layout)
//绑定控件id---recyclerView
val forecastList = findViewById(R.id.forecast_list) as RecyclerView
//为recyclerView创建LinearLayoutManager实例
forecastList.layoutManager = LinearLayoutManager(this)
//为recyclerView设置适配器
forecastList.adapter = ForecastListAdapter(items)
}
}
结果:
一个可以下拉的recyclerView
五,总结几点:
1,每个句子后面不需要添加分号---';'
2.定义参数时,类型在后边,名字在前面,中间加冒号,如
fun add(x: Int ){
}
3,不需要用new
好了,其他的再说