kotlin 類和對象_Kotlin程序| 類和對象的示例(帶有學生數據)

kotlin 類和對象

In the below program, we are creating a student class to input and print the student data like name, age. It is a simple example of creating class in Kotlin.

在下面的程序中,我們將創建一個學生班級,以輸入和打印學生數據,例如姓名,年齡。 這是在Kotlin中創建類的簡單示例。

Note:

注意:

  • The compiler creates a default constructor if we do not declare a constructor.

    如果我們不聲明構造函數,則編譯器將創建默認構造函數。

  • Class properties must be initialized.

    類屬性必須初始化。

Kotlin學生課程 (Program for student class in Kotlin)

package com.includehelp
// Class declaration,
class Student{
//member variables of class
private var name: String=""
private var age: Int=0
//Member functions of class to set student name
fun setStudentName(name:String){
this.name=name
}
//Member functions of class to set student age
fun setStudentAge(age:Int){
this.age=age
}
//Member functions of class to return student details
fun getStudentDetails():String{
return "Name :  $name, Age : $age"
}
}
//Main function, Entry Point of Program
fun main(args:Array<String>){
//Create Object of Student Class
val student1 = Student() // There is no 'new' keyword
// set Student age and name to call member functions of class
student1.setStudentName("Mike")
student1.setStudentAge(30)
//print Student details bt ccall getStudentDetails members functions
println("Student : ${student1.getStudentDetails()}")
//Create Second object of student class
val student2 = Student()
student2.setStudentName("Irina")
student2.setStudentAge(23)
println("Student : ${student2.getStudentDetails()}")
}

Output:

輸出:

Student : Name :  Mike, Age : 30
Student : Name :  Irina, Age : 23

翻譯自: https://www.includehelp.com/kotlin/example-of-class-and-object-with-student-data.aspx

kotlin 類和對象

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542383.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542383.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542383.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python 復數數組_python的數組運算及推導式的運用

運行方式&#xff1a;縮進 TAb(四格)或者space(1格)注釋&#xff1a;#可以在程序后注釋文字 或者 ... &#xff0c;""" ... """ 可以多行注釋 中間全為注釋數值輸出 print(...)圖形輸出 import matplotlib as pltplt.show()列表 list[] #字符串…

ActiveX: 如何用.inf和.ocx文件生成cab文件

ActiveX: 如何用.inf和.ocx文件生成cab文件 轉載于:https://www.cnblogs.com/time-is-life/p/5977962.html

Android工具里沒有Android,android – AppCompat工具欄沒有顯示

在主題中聲明.NoActionBar之后,以及將工具欄放在布局中,我的工具欄不會顯示.我最終得到的正是你在宣布沒有動作欄時所期望的 – 沒有動作欄.這是布局&#xff1a;activity_home.xml&#xff1a;layout"layout/app_bar_home"android:layout_width"match_parent&q…

操作系統中的處理機調度調度_操作系統中的多處理器調度

操作系統中的處理機調度調度多處理器操作系統 (Multiprocessor Operating system) A multiprocessor system consists of several processors which share memory. In the multiprocessor, there is more than one processor in the system. The reason we use multiprocessor …

轉:Delphi2010新發現-類的構造和析構函數功能

Delphi2010發布了. 雖然憑著對Delphi的熱愛第一時間就安裝了,但是現在可能是年紀大了,對新事物缺乏興趣了.一直都沒有仔細研究. 今天有點時間試了一下新功能. 本來C#和Delphi.NET是支持類的構造函數/析構函數的(注意不是實例的構造和析構).也就是在模塊初始化/卸載的時候會調用…

sed 替換_sed命令批量替換文件內容

“ 開發人員有時會大批量替換文件內容&#xff0c;sed命令是一個很好用的工具。”01—暴力替換方式近期有個臨時任務,將系統中所有"帳"替換為"賬",那"帳"和"賬"有啥區別呢;1、賬的部首是貝&#xff1b;帳的部首是巾。2、賬是關于貨幣、…

android 模仿uc標簽頁,模仿UCweb菜單 - 白羽雕弓 - 博客園

UCWeb的菜單看起來不錯&#xff0c;自己想模仿做一個&#xff0c;苦惱一直沒有思路google了幾天&#xff0c;終于找到一個帖子 http://www.eoeandroid.com/viewthread.php?tid28824按照上面提供的思路實現了1、保留menu按鍵作用2、用popupwindow作為菜單顯示容器3、用GridVie…

ipv6路由協議配置_IPV6尋址,標頭和路由協議

ipv6路由協議配置The IPV6 address is 128 bits. Whereas IPV4 is represented by 4 groups of decimal numbers, same IPV6 is represented by 8 groups of hexadecimal numbers. The example of IPV6 address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334. IPV6地址是128位。…

rpc框架

Motan&#xff0c;dubbo轉載于:https://www.cnblogs.com/zhangshiwen/p/5980886.html

android webview 監聽js,Android webview與js的數據交互

項目要用到Webview和js交互&#xff0c;查了查以前的項目感覺還是有必要整理下的。簡單描述下項目中用到的地方&#xff0c;比如說在web頁需要用到登錄的地方點擊登錄跳轉到APP原生登錄界面去登錄&#xff0c;點擊web頁的撥打電話彈出原生dialog詢問是否撥打&#xff0c;點擊we…

c ++查找字符串_C ++異常處理| 查找輸出程序| 套裝1

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){try {int num1 10;int num2 0;int res 0;res num1 / num2;}catch (exception e) {cout << "Exception: Divide By Zero" << endl;}return 0;}O…

python的repr和str有什么不同_str()和repr()的異同

str()函數和repr()函數&#xff0c;都是Python內置的標準函數。這兩個函數都是根據參數對象返回一個字符串&#xff0c;但是又有一些不一樣的地方。我們在使用的時候&#xff0c;常常搞混&#xff0c;傾向于使用簡單明了的str()函數&#xff0c;而搞不清楚為什么還有一個不知所…

android web通訊錄,Android手機開發之通訊錄

Android手機開發——通訊錄實現增加、查詢、修改、刪除的功能&#xff0c;輸入聯系人信息&#xff0c;點擊“添加”按鈕&#xff0c;可以添加聯系人信息到數據庫&#xff1b;點擊“查詢”按鈕&#xff0c;會發現添加的聯系人信息顯示在界面中&#xff1b;重新輸入聯系人電話&am…

有關UITableView--cell復用問題

近來用Tableview做了一個九宮格。過程中碰到了兩個cell復用問題。 問題一&#xff1a; 在cell中為button添加addTarget點擊事件時&#xff0c;出現后面的cell會重疊它前面cell的事件。代碼如下&#xff1a; C代碼 static NSString *CellWithIdentifier "DiscoverHomeTab…

python客戶端和服務端實驗_結合服務器和客戶端python

我正在嘗試使用python(稍后可能用c語言)和TCP套接字制作一個本地網絡聊天程序。我的目的是讓服務器監聽當前計算機的地址以獲取傳入消息&#xff0c;并將這些消息轉發給客戶端(我現在還不確定)。客戶端將是一個簡單的gui&#xff0c;可以通過本地連接向活動服務器發送消息。實際…

python常用語法和示例_C語言切換案例教程,語法,示例和規則

python常用語法和示例使用默認情況下的決策 (Decision making using switch-case-default) Many times in our daily lives, we face conditions where we are required to choose between a number of alternatives rather than just two or three. For example, which school…

android so abi適配,Android NDK學習(六): so文件兼容之abiFilters的使用

最近項目中遇到了要使用JavaCV的情況&#xff0c;涉及到了abi兼容的選擇。因為如果全部都適配的話&#xff0c;包很大&#xff0c;這樣兼容那些用戶數極少的cpu就很不劃算&#xff0c;所以我只適配了armeabi-v7a這一個。但是今天在x64-v8a的模擬器上看的時候&#xff0c;提示我…

python中doc=parased.getroot()_python中執行sed命令操作源文件時出現錯誤

我想在python中執行一個sed命令&#xff0c;第一種方法直接指定文件時&#xff0c;可以正確輸出結果&#xff0c;但是第二種我打開文件操作的時候就有問題&#xff0c;不知道什么原因&#xff0c;求高手解答&#xff1f;(1)>>> sedcmd"sed -n \s/{//g; p\ /qye/p…

JavaScript基礎之Number對象和Math對象

2019獨角獸企業重金招聘Python工程師標準>>> //Math對象//屬性float Math.E; //返回自然對數的底數e&#xff0c;約2.718float Math.LN2; //返回2的自然對數&#xff0c;約0.693float Math.LN10; //返回10的自然對數&#xff0c;約2.302fl…

c++ stl 獲取最小值_如何在C ++ STL中找到向量的最小/最小元素?

c stl 獲取最小值Given a vector and we have to minimum/smallest element using C STL program. 給定一個向量&#xff0c;我們必須使用C STL程序最小/最小元素。 尋找向量的最小元素 (Finding smallest element of a vector) To find a smallest or minimum element of a …