1. The nested class
/** * class can be nested in other classes ** /
/ / news
class News{
// Default locale
private var lang ="cn"
// News classification: nested class, and the main class is not very close, just a form of cooperation
class Category{
var list = arrayOf("Recommended"."Video"."Hot spots"."Science and technology"."The beauty")
val listDesc =list.joinToString()
}
// Inner class: news language, usually used for classes that are not directly external, main class service
inner class Language{
fun changeRegion(newRegion: String){
// The inner class can access the attributes of the main class
lang =newRegion
println("You can check$newRegion"+ "Regional news")}}}fun main(args: Array<String>) {
// Displays a list of news items in China
println(News.Category().listDesc)
// To change the news locale, the inner class must have an instance of the main class, plus its own instance, to use it
News().Language().changeRegion("us")}Copy the code
Recommendations, videos, hot topics, technology, and beautiful women can check out US regional newsCopy the code
2. Data classes
/** * class can be nested in other classes ** /
/ / news
class News{
// Default locale
private var lang ="cn"
// News classification: nested class, and the main class is not very close, just a form of cooperation
class Category{
var list = arrayOf("Recommended"."Video"."Hot spots"."Science and technology"."The beauty")
val listDesc =list.joinToString()
}
// Inner class: news language, usually used for classes that are not directly external, main class service
inner class Language{
fun changeRegion(newRegion: String){
// The inner class can access the attributes of the main class
lang =newRegion
println("You can check$newRegion"+ "Regional news")}}}fun main(args: Array<String>) {
// Displays a list of news items in China
println(News.Category().listDesc)
// To change the news locale, the inner class must have an instance of the main class, plus its own instance, to use it
News().Language().changeRegion("us")}Copy the code
Recommendations, videos, hot topics, technology, and beautiful women can check out US regional newsCopy the code
3. The enumeration class
/** * Enumeration classes: a finite number of types of enumeration *, each of which is called an enumeration constant (with an initial value), separated by commas */
// Card suit
enum class PokerCard{hearts, diamonds, clubs, spades}// Clothes size: enumeration class with constructor
enum class Size(val height:Int)
{
S(150),M(160),L(170),XL(180),XXL(190)}fun main(args: Array<String>) {
// Enumerates the constants of the enumerated class
println(PokerCard.values().joinToString())
// Enumerate constants
println(Size.valueOf("XXL").name)
// Enumerate constant sort
println(Size.valueOf("XXL").ordinal)
println(Size.values().joinToString { it.name +":"+it.height })
}
Copy the code
Hearts, diamonds, clubs, spades XXL4
S:150, M:160, L:170, XL:180, XXL:190
Copy the code