面向对象的C#语言——类、字段、方法
现实中的事物抽象为类
- 类(Class)最基本的要素是:字段(field)—变量,方法(method)—函数.
例:
Person(类):
_ 字段:age,name
_ 方法:GetInfo,IsOlderThan,SayHello
一、定义类中的字段与方法
定义一个Person(人)类:
Class Person(){
//字段—变量
public String name;
public int age;
//方法—函数
public void SayHello(){
//void 无返回值
Console.WriteLine("Hello! My name is"+name);
}
public String GetInfo(){
//String有返回值
return "Name:"+name+",Age:"+age;
}
}
二、构造方法(Constructor)
构造方法的主要作用是完成对象的初始化工作
(1)构造方法的方法名与类名相同
(2) 构造方法没有返回值类型,也不能写void
public Person(String n,int a){
name = n;
age = a;
}
- 默认(default)构造方法
如果用户没有定义任何构造方法,则系统会自动生成一个public person(){}
三、对象的创建
- 构造方法不能够显式地直接调用,而应该用new 来调用
- Person p = new Person(“Mr_singtop”,22);
四、对象的使用
Person p = new Person(“singtop”,22);
Console.WriteLine(p.name);//使用Person对象中的name变量
p.SayHello();//使用Person对象中的SayHello()方法
析构方法(释放对象)
Class Person(){
…
~Person(){
…//释放对象
}
}
由于C#自动进行对象的释放,所以用户一般不定义析构结构
五、方法的重载(OverLoading)
public void SayHello(){
Console.WriteLine("Hello! My name is"+name);
}
public void SayHello(Person another){
//运用另一个对象调用该方法
Console.WriteLine("Hello,"+another.name+"!My name is "+name);
}
方法的签名:方法名及参数个数及类型构成(参数名不算)
六、使用this
this指对象本身,常用于:
- 访问对象本身的字段与方法
- 区分字段与局部变量
例:
Public Person(string name,int age){
this.name = name;
this.age = age;
}
- 用于构造方法调用另一个构造方法,注意其位置
例:
public Person() : this (0,"")
{
//构造方法的其他语句;
}
七、应用实例
银行系统
- 系统中有几类对象?
Account(账户),Bank(银行),ATM(自动取款机) - 每个类中有什么字段、方法?
类 Account(class):
_ 字段:id,money,pwd
_ 方法:Account,getId,getMoney,getPwd,IsMath(id与密码是否匹配)
SaveMoney,setId,setMoney,setPwd,WithDrawMoney(账户交易)
类 Bank(class):
_ 字段:accounts(记录所有账户集合List<Account> accounts = new List<Account>();
_ 方法:closeAccount(开账户),FindAccount(查找账户),OpenAccount(关闭账户)
类 ATM(class):
_ 字段:bank (关联银行)
_ 方法:ATM,GetInPut(用户信息输入),Show(用户信息显示),Transaction(交易)
注:可在VS中添加"类关系图"(类图)
代码:
账户对象——
//创建Account(账户)类——字段、方法
public class Account
{
/**
* 账户(Account)——字段
* */
//string name;//账户名
string id;//账户id
string pwd;//账户密码
double money;//账户金额
/**
* 账户(Account)——构造方法
* */
public Account(string id,string pwd,double money) {
this.id = id;
this.pwd = pwd;
this.money = money;
}
/**
* 账户(Account)——方法
* */
//获取账户ID
public string getId()
{
return id;
}
//获取账户金额
public double getMoney()
{
return money;
}
//获取账户密码
public string getPwd()
{
return pwd;
}
//检验账户id与账户密码是否对应(判断该账户是否存在)
public bool IsMath(string id,string pwd)
{
if (this.id == id && this.pwd == pwd)
{
return true;
}
else
{
return false;
}
}
//保存金钱
public bool SaveMoney(double money)
{
if (this.money > 0)//当账户余额为正值时,可以向账户中存钱
{
this.money += money;
return true;//存钱成功
}
else
{
return false;//存钱失败
}
}
//设置id
public void setId(string id)
{
this.id = id;//将字段id赋值于对象id
}
//设置密码
public void setPwd(string pwd)
{
this.pwd = pwd;
}
//设置金额
public void setMoney(double money)
{
this.money = money;
}
//取钱
public bool WithDrawMoney(double money)
{
if (this.money >= money)//当要取的金额小于账户的金额时,可以取钱
{
this.money -= money;//取钱后余额
return true;//取钱成功
}
else
{
return false;//取钱失败
}
}
}
银行对象——
using System;
using System.Collections.Generic;
//创建银行对象(class)——字段、方法
public class Bank
{
/**
* 创建银行对象中的字段
* */
//银行中包含多个账户,建立账户列表(核心)
List<Account> Accounts = new List<Account>();
/**
* 创建银行对象中的方法
* */
//创建账户(开账户)
public Account OpenAccount(string id,string pwd,double money) //返回值为Account对象
{
Account account = new Account(id,pwd,money);//创建账户对象
Accounts.Add(account);//将创建的账户对象添加到庄户列表中
return account;
}
//查找账户
public Account FindAccount(string id, string pwd)
{
foreach(Account account in Accounts)//依次查询Accounts账户列表中的账户
{
if (account.IsMath(id, pwd))//判断账户id与pwd是否存在于账户列表中
{
return account;
}
}
/**
* 采用for循环遍历列表
* for(int i = 0; i < Accounts.Count; i++)
* {
* Account account = Accounts[i];
* if (account.IsMath(id, pwd))
* {
* return account;
* }
* }
* */
return null;
}
//关闭账户
public bool CloseAccount(Account account)
{
int idx = Accounts.IndexOf(account);//查找Accounts列表中是否有account对象的存在,并返回该对象在列表中的索引值。
if (idx >= 0)//account账户(对象)存在于Account列表中
{
Accounts.Remove(account);
return true;//关闭账户成功
}
return false;//关闭账户失败
}
}
ATM对象——
using System;
using System.Collections;
//创建ATM对象——字段、方法
public class ATM
{
/**
* ATM对象中的字段
* */
Bank bank;
/**
* ATM对象的构造方法
* */
public ATM(Bank bank)
{
this.bank = bank;
}
/**
* ATM对象中的方法
* */
public string GetInPut()//账户信息输入
{
return Console.ReadLine();//输入信息
}
public void Show(string msg)//账户信息展示
{
Console.WriteLine(msg);//信息展示
}
/**
* ATM机交易
* */
public void TransAction()
{
Show("请输入账户ID:");
string id = GetInPut();
Show("请输入账户密码:");
string pwd = GetInPut();
Account account = bank.FindAccount(id, pwd);//通过id和pwd查询银行中是否存在该账户
if(account == null)
{
//账户不存在
Show("您输入的账户不存在!");
return;
}
else
{
int x = 1;
while (x == 1)
{
Show("\n\n\nSaveMoney:1,WithDrawMoney:2,Display:3_(存钱请按1,取钱请按2,账户信息查询请按3):");
int op = int.Parse(GetInPut());//将输入的字符串型转化为整形并赋值于op
if (op == 1)//当输入1时,进行存钱操作
{
Show("请您输入要存的金额:");
double money = double.Parse(GetInPut());//输入存钱金额,并将其转化为double型
//调用Account对象中的存钱函数SaveMoney
bool result = account.SaveMoney(money);
if (result)
{
Show("您的金钱已存入账户!");
}
else
{
Show("您的存钱操作出错,请重新操作!");
}
}
else if (op == 2)//当输入2时,进行取钱操作
{
Show("请您输入要取的金额:");
double money = double.Parse(GetInPut());//输入取钱金额,并将其转化为double型
bool result = account.WithDrawMoney(money);
if (result)
{
Show("您的金钱已取出,请收好!");
}
else
{
Show("您的取钱操作出错,请重新操作!");
}
}
else if (op == 3)//当输入3时,进行账户信息查询操作
{
string id1 = account.getId();
double money1 = account.getMoney();
Show("尊敬的" + id1 + "用户,您的账户余额为:" + money1);
}
else
{
Show("输入错误,请按要求输入!");
}
Console.WriteLine("是否继续进行交易(1:继续,0:结束):");
x = int.Parse(GetInPut());
}
}
}
}
主函数——
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankSystem
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("------------银行系统(ATM机)-----------");
Console.WriteLine("----------------选择银行--------------");
Console.WriteLine("A:创建账户 B:查找账户 ");
Console.WriteLine("C:关闭账户 D:账户交易 ");
Console.WriteLine("E:修改密码 G:继续 ");
string[] BankNames = { "工商银行", "邮政银行", "农业银行" };
Console.WriteLine("\n\n\n请选择银行:");
string BN = Console.ReadLine();
int m = 0;
for (int i = 0; i < BankNames.Length; i++)//循环查找数组中是否有该银行
{
if (BN == BankNames[i])//存在选择的银行
{
Bank bank = new Bank();//创建银行对象
String OP = "G";
while (OP == "G")//当OP为"G"时,进行循环操作,当不为G时,跳出循环
{
Console.WriteLine("\n\n----------欢迎来到" + BN + "!--------");
Console.WriteLine("请根据要求选择要进行的操作:");
OP = Console.ReadLine();//当OP为A时,进行创建账户
if (OP == "A")
{
Account account = null;//创建空账户对象
Console.WriteLine("\n\n--------创建账户--------");
//输入账户ID,pwd,money,进行创建
Console.WriteLine("请输入账户ID:");
string id = Console.ReadLine();
Console.WriteLine("请输入账户密码:");
string pwd = Console.ReadLine();
Console.WriteLine("请输入账户金额:");
double money = double.Parse(Console.ReadLine());
account = bank.OpenAccount(id, pwd, money);//开账户,调用账户创建函数,并将返回值赋予创建的空账户对象
if (account != null)//当创建的账户对象(account)不再为null时,账户创建成功
{
Console.WriteLine("账户创建成功!");
}
else//当创建的账户对象(account)还是为空时,失败
{
Console.WriteLine("账户创建失败!");
}
}
else if (OP == "B")
{
Account account = null;
Console.WriteLine("\n\n--------查找账户--------");
//输入账户id,pwd进行查找
Console.WriteLine("请输入账户ID:");
string id = Console.ReadLine();
Console.WriteLine("请输入账户密码:");
string pwd = Console.ReadLine();
account = bank.FindAccount(id, pwd);//调用查找账户函数,将返回的account类型的值赋予创建的空账户对象
if (account != null)//赋值后的账户对象不再为null时,账户存在
{
Console.WriteLine("该账户存在!");
Console.WriteLine("账户ID:" + account.getId() + ";账户金额:" + account.getMoney());
}
else//赋值后的账户依然为null时,账户不存在
{
Console.WriteLine("该账户不存在!");
}
}
else if (OP == "C")
{
Account account = null;
Console.WriteLine("\n\n--------关闭账户--------");
//输入账户id,pwd查找该账户是否存在,存在时并将其关闭
Console.WriteLine("请输入账户ID:");
string id = Console.ReadLine();
Console.WriteLine("请输入账户密码:");
string pwd = Console.ReadLine();
account = bank.FindAccount(id, pwd);
if (account != null)//账户存在
{
bool result = bank.CloseAccount(account);//关闭账户
if (result)
{
Console.WriteLine("账户关闭成功!");
}
else
{
Console.WriteLine("账户关闭失败!");
}
}
else
{
Console.WriteLine("您输入的账户不存在,请仔细检查!");
}
}
else if (OP == "D")
{
ATM atm = new ATM(bank);//根据创建的Bank对象创建ATM对象,进行交易
Console.WriteLine("\n\n--------账户交易--------");
atm.TransAction();//调用ATM对象中的交易方法
}
else if (OP == "E")
{
Account account = null;
Console.WriteLine("\n\n--------修改密码--------");
//输入账户id,pwd查找该账户是否存在,若存在,进行密码修改
Console.WriteLine("请输入账户ID:");
string id = Console.ReadLine();
Console.WriteLine("请输入账户密码:");
string pwd = Console.ReadLine();
account = bank.FindAccount(id, pwd);//查找账户是否存在
if (account != null)//存在
{
Console.WriteLine("请输入新的账户密码:");
string pwd1 = Console.ReadLine();//输入新密码
account.setPwd(pwd1);//更改密码
if (account.getPwd() == pwd1)//更改成功(账户对象密码等于输入的新密码)
{
Console.WriteLine("账号密码修改成功!");
}
else
{
Console.WriteLine("账号密码修改失败!");
}
}
}
Console.WriteLine("是否继续操作(G继续,其他任意键结束):");
OP = Console.ReadLine();//控制是否循环
}
m = 1;//将m修改为1,代表银行数组BankNames中含有选择的银行
}
}
if (m == 0)//当m未被修改时,输入的银行不存在
{
Console.WriteLine("该银行不存在!");
}
}
}
}
运行结果:
@梦幻泡沫