-
如何更好地理解封装
查看全部 -
public 是公共字段,可以在类外被修改
private 是私有字段,无法在类外访问
查看全部 -
用return带回方法的返回值需将void改成返回值的类型,void代表方法没有返回值 设置返回值 int sum
查看全部 -
return;//结束方法<br> return sum;//返回值 void 没有返回值
查看全部 -
////porgram
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xuexi1
{
class Program
{
static void Main(string[] args)
{
/* Child xiaoMing = new Child();//名+对象名+关键字+类。实列化Child类的对象
xiaoMing.Name = "马晓明"; //对象名.字段名=赋值
//xiaoMing.Sex = "男";
xiaoMing.Age = 6;
//xiaoMing.Height = 120;
Console.WriteLine("我叫" + xiaoMing.Name + ",今年" + xiaoMing.Age + "岁。");
xiaoMing.PlayBall();//对象名.方法名。调用踢球的方法 */
Child child = new Child(); //声明和实列化类的对象
/*child.PlayBall();//对象名.方法名 调用方法
child.EatSugar("水果糖");//方法的实参,sugar="榴莲糖" 隐藏了赋值语句*/
child.EatSugar("牛奶糖");
child.EatSugar(4);
child.EatSugar("水果", 5);
}
}
}
////child.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xuexi1
{
class Child
{//是访问修饰符 public公共的 可访问可修改 privte私有的,隐藏(默认的)
private string _name;//姓名
public string Name //属性 +首字母大写名字
{
get { return _name; } //读访问器
set { _name = value; }//写访问器
}
public string Sex { get => sex; set => sex = value; }
public int Age { get => age; set => age = value; }
public int Height { get => height; set => height = value; }
//CTRL+R+E 封装快捷键
private string sex = "男";//性别
private int age;//年龄
private int height;//身高
public void PlayBall()//方法的声明
{//方法体 方法的实现
Console.WriteLine("耶!我是小小C罗!");
}
/// <summary>
/// 吃糖定义
/// </summary>
///<param name="sugar">糖的类型(形参)</param>
public void EatSugar(string sugar)
{
if (sugar == "榴莲糖")
Console.WriteLine("哎呀,最怕榴莲的味道了!");
else
Console.WriteLine("哈哈,是我喜欢吃的糖!");
}
/// <summary>
/// 吃糖 同意类中,多个方法名字相同的但参数不同(类型或数量)==重载方法调用
/// </summary>
/// <param name="count">糖的数量</param>
public void EatSugar(int count)
{
if (count > 3)
Console.WriteLine("吃糖太多对牙齿不好!");
else
Console.WriteLine("吃吧,吃糖糖吧");
}
/// <summary>
/// 吃糖
/// </summary>
/// <param name="sugar">糖的类型</param>
/// <param name="count">糖的数量</param>
public void EatSugar(string sugar, int count)
{
if (sugar == "牛奶糖" && count > 2)
Console.WriteLine("牛奶糖不能吃太多哦!");
else if (count > 3)
Console.WriteLine("糖不能吃太多了");
else
Console.WriteLine("吃糖糖吧!");
}
}
}
查看全部 -
值类型 引用类型
查看全部 -
用对象初始化器初始化对象 必须有一个无参构造方法
查看全部 -
this 当前对象
查看全部 -
Child c1= new Child()
c1.Name="某某"
Child c2=c1; //只是把地址传递给c2
c2,.Name="某某某"//把地址中的值改变了 并没哟重新开辟一个地址
查看全部 -
Ctrl+r+e
查看全部 -
对齐快捷键 ctrl+k+d,三个键同时按下
查看全部 -
return 结束方法调用
查看全部 -
F5 运行 F11 跳转到定义
查看全部 -
结构是值类型,类是引用类型;
查看全部 -
无参调用构造方法:
查看全部
举报