...
using System;
// 面向?qū)ο笕筇匦裕悍庋b 繼承 多態(tài)
// 人類
// 類是統(tǒng)稱
// 聲明
class Person
{
// 訪問控制 可以限定變量在什么地方可以訪問的到
// public 公共的 任何地方都可以訪問的到
public string name;
public string sex;
public int age;
public float height;
public float weight;
// 方法
// 1坯屿,無參數(shù) 無返回值
public void SayHi ()
{
Console.WriteLine ("Hi!");
}
// 2. 有參數(shù) 無返回值
public void AddAge (int n)
{
age += n;
}
// 3. 無參數(shù) 有返回值
public int NominalAge ()
{
return age + 1;
}
// 4. 有參數(shù) 有返回值
public float sum (float f1, float f2)
{
float s = f1 + f2;
return s;
}
}
// 手機(jī)類
class Phone
{
// 這里的變量就是物體的特征
// 一個(gè)物體的特征有無數(shù)個(gè)愉豺、只挑編程用到的信息來寫
// 字段(實(shí)例變量)
// 價(jià)格
public float price;
// 牌子
public string brand;
// 內(nèi)存大小
public int memorySize;
// 顏色
public string color;
// 屏幕尺寸
public int screenSize;
// CPU
public string cpu;
// 系統(tǒng)
public string OS;
}
// 寫一個(gè)英雄類,并考慮有多少字段。寫出來
class Hero
{
// 血量
public int HP;
// 藍(lán)量
public int MP;
// 名字
public string name;
// 等級(jí)
public int level;
// 經(jīng)驗(yàn)
public int Exp;
// 攻擊力
public float attack;
// 護(hù)甲值
public float armor;
// 移速
public float speed;
// 法術(shù)強(qiáng)度
public float spellPower;
// 魔抗
public float spellResistance;
// 錢財(cái)
public float wealth;
// 裝備
public string equip;
}
/*********************************************/
class BlackBoard
{
public float width;
public float height;
public string color;
public void Write (string str)
{
Console.WriteLine ("I write this:" + str);
}
}
class Computer
{
float price;
string brand;
bool startingUp;
public void Starting ()
{
if (!startingUp) {
startingUp = true;
Console.WriteLine ("Starting up is successful!");
} else {
Console.WriteLine ("The computer is turned on!");
}
}
public void Shutdown ()
{
if (startingUp) {
startingUp = false;
Console.WriteLine ("Shutdown is successful!");
} else {
Console.WriteLine ("The computer has been turned off!");
}
}
}
class Calculator
{
float result = 0;
public void Sum (float f1)
{
result += f1;
Console.WriteLine ("結(jié)果為:" + result);
}
public void Calculation ()
{
while (true) {
string sign = Console.ReadLine ();
float f = float.Parse (Console.ReadLine ());
switch (sign) {
case "+":
Sum (f);
break;
case "-":
break;
case "*":
break;
case "/":
break;
default:
break;
}
}
}
}
class MainClass
{
public static void Main (string[] args)
{
// int num = 100;
// 對(duì)象 是存在的個(gè)體
// Person p = new Person ();
// p.name = "Lee";
// p.sex = "男";
// p.age = 24;
// p.height = 185.2f;
// p.weight = 56.2f;
//
// Console.WriteLine (p.name);
// Console.WriteLine (p.age);
// // 目前學(xué)的保存數(shù)據(jù)的兩種手段:數(shù)組 變量
// // 局限 基本數(shù)據(jù)類型變量 無法大量保存
// // 數(shù)組 必須是相同類型的
// // 人: 姓名 性別 年齡 身高 體重...
// string name = "張三";
// string sex = "男";
// int age = 19;
// float height = 1.89f;
// float weight = 100f;
//
// Phone phone1 = new Phone ();
// phone1.brand = "蘋果";
//
// Phone phone2 = new Phone ();
// phone2.brand = "小米";
//
// Hero h = new Hero ();
// h.name = "Class、Lee";
// h.HP = 100;
// h.MP = 100;
// h.level = 1;
// h.Exp = 0;
//
// Hero h1 = new Hero ();
// h1.name = "荊軻";
// h1.HP = 100;
// h1.MP = 100;
// h.level = 1;
// h.Exp = 0;
// 變量 生命周期和作用域
// 生命周期: 從內(nèi)存分配,到內(nèi)存收回(變量創(chuàng)建,到變量銷毀)
// 作用域: 這個(gè)變量可以用在什么地方
// 一般來說一個(gè){}就是一個(gè)作用域
// 一個(gè)變量的作用域是從創(chuàng)建開始誊垢,到它所在的 {} 結(jié)束
// int test = 10;
// {
// Console.WriteLine (test);
// }
//
// { int test1 = 10; }
// Console.WriteLine (test1);
// // 方法調(diào)用
// Person p = new Person ();
// p.age = 10;
// p.SayHi ();
// p.AddAge (8);
// Console.WriteLine (p.age);
// int nAge = p.NominalAge ();
// Console.WriteLine (p.NominalAge ());
// float a = 7;
// float b = 3.3f;
// float f = p.sum (a, b);
// Console.WriteLine (p.sum (a, b));
// BlackBoard b = new BlackBoard ();
// b.width = 8.2f;
// b.height = 1.3f;
// b.color = "black";
// b.Write ("Hello world");
// Computer c = new Computer ();
// c.Starting ();
// c.Starting ();
Calculator c = new Calculator ();
c.Calculation ();
}
}
...