using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Base07
{
class Program
{
static void Main(string[] args)
{
//對象 萬事萬物皆為對象
//基本的特征 類
//特征=屬性 動作=方法
//需要使用到類中的屬性 1 執行個體化類對象 2 用類對象的别名來進行屬性的指派
//Person p = new Person();
//Person p = new Person("zs", 1);
//Person p = new Person("zs");
// p._pname = "zs";
//p.pid = 1;
//p.ShowPname();
//Console.WriteLine(p.getPname());
//Console.WriteLine(p.getPname("1234"));
//p.ShowPanames();
// Console.WriteLine("{0}:{1}",p.pname,p.pid);
//值類型 引用類型
//引用類型 new 堆
//值類型 (基礎的資料結構,類型) 無new 棧
//結構
//Person person;
//person.pid = 1;
//Console.WriteLine(person.pid);
//Console.ReadKey();
}
}
}
--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Base07
{
struct Persons
{
public string pname;
public int pid;
}
}
---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Base07
{
//通路修飾符
//公共 私有 受保護 預設的修飾符
//修飾符通路的範圍
//公共 所有都可以進行通路
//私有 目前的類中可以被通路
//受保護 子類可通路
//預設的修飾符 目前的類庫中可以被通路
class Person
{
//構造函數 1無參(無參) 2有參 構造函數可以重載
/// <summary>
/// 無參的構造函數
/// </summary>
//public Person()
//{
// Console.WriteLine("無參的構造方法1");
//}
//public Person(string pname,int pid)
//{
// this._pname = pname;
// this._pid = pid;
// Console.WriteLine("帶參數的構造函數");
//}
//public Person(string pname)
//{
// this._pname = pname;
// this._pid = 2;
// Console.WriteLine("帶參數的構造函數重載");
//}
/ <summary>
/ 釋放資源
/ </summary>
//~Person(){
// this._pname = null;
// this._pid = 0;
// Console.WriteLine("析構函數");
//}
//public string _pname;
//public int _pid;
方法 四類
同名不同參 與傳回值無關 方法的重載
1 無參無傳回
//public void ShowPname()
//{
// Console.WriteLine(_pname);
//}
2 有參無傳回
//public void SHowpname(string pname)
//{
// if (pname.Length > 3)
// {
// Console.WriteLine(pname);
// }else
// {
// Console.WriteLine(this._pname);
// }
//}
3 無參有傳回
傳回值的類型 有方法定義的資料類型來決定
//public string getPname()
//{
// if (_pname.Length > 3)
// {
// return _pname;
// }
// return "預設值";
//}
4 有參有傳回
//public string getPname(string pname)
//{
// if (_pname.Length > 3)
// {
// _pname=pname;
// }
// return _pname;
//}
//public void ShowPanames()
//{
// Console.WriteLine(getPname("1234"));
//}
}
}
寫一個狗的類,類中有屬性:姓名,性别,品種。(使用3個構造方法進行對象的初始化)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Person
{
class Dog
{
//public string _name;
//public string _sex;
//public string _type;
(一)無傳回,無參數的方法:
狗跑步的方法,要求輸出“某某品種的狗跑得好快”。
//public void run()
//{
// Console.WriteLine(_type + "的狗跑得好快");
//}
狗長大的方法,要求輸出“某某姓名的狗長大了”。
//public void grow()
//{
// Console.WriteLine("姓名:" + _name + "的狗長大了");
//}
(二)有傳回,無參數的方法:
得到狗的姓名方法,要求傳回目前狗的姓名。
//public string Name()
//{
// Console.WriteLine("狗的姓名:"+_name);
// return _name;
//}
得到狗的性别的方法,要求傳回目前狗的性别。
//public string Sex()
//{
// Console.WriteLine("狗的性别:" + _sex);
// return _sex;
//}
得到狗的品種的方法,要求傳回目前狗的品種。
//public string Type()
//{
// Console.WriteLine("狗的品種" + _type);
// return _type;
//}
傳回目前對象資訊的方法,即toString()方法。
//public string toString()
//{
// //Console.WriteLine("姓名:{0},性别:{1},品種:{2}",_name,_sex,_type);
// return "姓名:" + _name +","+ "性别:" + _sex +","+ "品種:"+","+ _type;
//}
(三)無傳回,有參數的方法:
設定目前狗的姓名的方法,要求傳入狗的新姓名,修改目前姓名。
//public void setName(string name)
//{
// _name = name;
//}
設定目前狗的性别的方法,要求傳入狗的新性别,修改目前性别。
//public void setSex(string sex)
//{
// _sex = sex;
//}
設定目前狗的品種的方法,要求傳入狗的新品種,修改目前品種。
//public void setType(string type)
//{
// _type = type;
//}
}
}
寫一個測試類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Person
{
class Program
{
static void Main(string[] args)
{
// 寫一個測試類,要求首先生成一隻狗,使用設定方法将狗的屬性設上值,再調用狗跑步的方法,
// 調用狗長大的方法。再調用得到狗姓名的方法,接收傳回,并輸出傳回的姓名。
// 最後使用輸出,将這隻狗的所有資訊輸出。
//Dog d = new Dog();
//d._name = "好好";
//d._sex = "公";
//d._sex = "母";
//d._type = "土狗";
//d.run();
//d.grow();
//Console.WriteLine(d.Name());
//Console.WriteLine(d.toString());
//Console.ReadKey();
}
}
}