Initial commit: C# 学习笔记和示例代码
- Lesson 01-10: C# 基础语法 - WebView2: 集成示例 - notes/: 详细笔记
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson05.Models
|
||||
{
|
||||
class Person
|
||||
{
|
||||
// public: 任何地方都能访问
|
||||
public string name;
|
||||
|
||||
// private: 只有本类内部能访问
|
||||
private int age;
|
||||
private string secret = "秘密信息";
|
||||
|
||||
// Property: 受保护的访问方式
|
||||
public int Age
|
||||
{
|
||||
get { return age; }
|
||||
set
|
||||
{
|
||||
// value 是关键字,表示赋值时的右值
|
||||
if (value < 0) value = 0;
|
||||
if (value > 150) value = 150;
|
||||
age = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 构造函数
|
||||
public Person(string name, int age)
|
||||
{
|
||||
this.name = name;
|
||||
this.Age = age; // 通过 Property 设置,自动限制范围
|
||||
}
|
||||
|
||||
public void SayHi()
|
||||
{
|
||||
Console.WriteLine($"我是 {name},今年 {age} 岁");
|
||||
Console.WriteLine($"我的秘密: {GetSecret()}"); // 本类内可以调用私有方法
|
||||
}
|
||||
|
||||
// 私有方法:本类内部使用
|
||||
private string GetSecret()
|
||||
{
|
||||
return secret;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user