kotlin 类和对象

 

类:抽象的概念,具有某些特征的事物

举例:人,狗,猪,楼,城市

class <类名> {

<成员>

}

对象:具体的个体

举例:jingwen,我家这小黄狗,橡树湾301号楼,北京

对象属于类,一个类可以有很多个对象

/**
 * Created by jingwen on 19/4/13.
 */

class Girl(var appearance:String,var character:String,var voice:String){
    init {
        println("this girl :appearance :$appearance character:$character voice:$voice")
    }
}

class Boy(var appearance:String,var character:String,var voice:String){
    init {
        println("this boy :appearance :$appearance character:$character voice:$voice")
    }

}

fun main(args: Array<String>) {
    var girl:Girl = Girl("pretty","gentle","sweet")
    var boy:Boy = Boy("handsome","gentle","sweet")
}
this girl :appearance :pretty character:gentle voice:sweet
this boy :appearance :handsome character:gentle voice:sweet

Process finished with exit code 0

 

继承

//被继承的类需要使用open修饰

/**
 * Created by jingwen on 19/4/13.
 */

class Girl(appearance:String, character:String, voice:String):Person(appearance,character,voice)
class Boy(appearance:String, character:String, voice:String):Person(appearance,character,voice)

open class Person(var appearance:String,var character:String,var voice:String){
    init {
        println("this $this.javaClass.name :appearance :$appearance character:$character voice:$voice")
    }
}

fun main(args: Array<String>) {
    var girl1:Girl = Girl("pretty","gentle","sweet")
    var boy1:Boy = Boy("handsome","gentle","high")

}

输出结果:

this Girl@24d46ca6.javaClass.name :appearance :pretty character:gentle voice:sweet
this Boy@4517d9a3.javaClass.name :appearance :handsome character:gentle voice:high

所有类都有一个共同体的祖宗Any

/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kotlin

/**
 * The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
 */
public open class Any {
    /**
     * Indicates whether some other object is "equal to" this one. Implementations must fulfil the following
     * requirements:
     *
     * * Reflexive: for any non-null reference value x, x.equals(x) should return true.
     * * Symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
     * * Transitive:  for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true
     * * Consistent:  for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
     *
     * Note that the `==` operator in Kotlin code is translated into a call to [equals] when objects on both sides of the
     * operator are not null.
     */
    public open operator fun equals(other: Any?): Boolean

    /**
     * Returns a hash code value for the object.  The general contract of hashCode is:
     *
     * * Whenever it is invoked on the same object more than once, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
     * * If two objects are equal according to the equals() method, then calling the hashCode method on each of the two objects must produce the same integer result.
     */
    public open fun hashCode(): Int

    /**
     * Returns a string representation of the object.
     */
    public open fun toString(): String
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值