版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
文章目录
2_4如何定义静态方法,静态方法有何特点?
用static修饰的方法是仅属于类的静态方法。与此同时,不用static修饰的方法,则为实例方法。
静态方法的本质是该方法是属于整个类的,不是属于某个实例的;静态方法效率上要比实例化高,但静态方法的缺点是不自动进行销毁,这可能会造成内存资源的浪费,而实例化的则可以做销毁;静态方法和静态变量创建后始终使用同一块内存空间,而使用实例的方式则会创建多个内存空间。
2_8 什么是访问控制符?有哪些访问控制符?哪些可以用来修饰类?哪些用来修饰域和方法?试述不同访问控制符的作用。
访问控制符是一组限定类、域或方法是否可以被程序里的其他部分访问和调用的修饰符。
C# 中的访问控制符有5个,其中基本的有4个:public,protected,private,internal,还有一个复合的修饰符protected internal(也可以写成internal protected)。
类的访问控制符只有一个public,域和方法的访问控制符有五个,分别是public、private、protected、internal、protected internal。
访问控制符表("Yes"表示可以访问)
访问控制符 | 同类中 | 相同程序集的子类 | 相同程序集的非子类 | 不同程序集的子类 | 不同程序集的非子类 |
---|---|---|---|---|---|
public | Yes | Yes | Yes | Yes | Yes |
protected internal | Yes | Yes | Yes | Yes | |
protected | Yes | Yes | Yes | ||
internal | Yes | Yes | |||
private | Yes |
3_1 编写一个C#程序定义一个表示学生的类student,包括域“学号”“班号”“姓名”“性别”“年龄”;方法“获得学号”“获得班号”“获得性别”“获得年龄”“修改年龄”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3_1
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.setAge(20);
Console.WriteLine(stu.getAge());
Console.ReadKey();
}
}
public class Student
{
//域(Field),就是字段
//编写一个C#程序定义一个表示学生的类student,包括域“学号”“班号”“姓名”“性别”“年龄”;方法“获得学号”“获得班号”“获得性别”“获得年龄”“修改年龄”。
public string SN;
public string getSN(){ return SN; }
public string CN;
public string getCN() { return CN; }
public string Name;
public string getName() { return Name; }
public string Sex;
public string getSex() { return Sex; }
public double Age;
public double getAge() { return Age; }
public void setAge(double v) { Age=v; }
}
}
3_2 综合练习:编写银行ATM程序。要求如下
(1) 使用面向对象的思想,模拟现实世界中的银行、账号、ATM等对象,其中类中有字段、方法;
(2)在程序中适当的地方,使用属性、索引器,注意使用修饰符;
(3)使用继承,继承账号(Account类)得到一个子类(如信用账号),增加字段(如信用额度)、属性、方法,覆盖(override)一些方法(如WithdrawMoney)。
(4)根据程序的需要(可选做),使用C#的其他语法成分,诸如:接口、结构、枚举等。
(5)程序中加上适当的注释,并加一个说明文件,简要描述在什么地方使用了一些特殊的语法要素。
using System;
namespace _3_2_综合练习_编写银行ATM程序
{
class Account
{
//Constructor
Account() { }
Account(long bank_cardID, string password)
{
this.bank_cardID = bank_cardID;
this.account_password = password;
}
//fields
private static long initial_ID = 1000000001; //the 1st one to create an account get this ID number
private static string bank_name = "ICBC";
private long bank_cardID;
public string account_password;
private long total_amount = 100000; //initial account
private string[] data = new string[5];
private string[] keys =
{
"card ID","holder's name", "total sum", "latest withdraw","latest deposit"
};
//property
public long latest_withdraw { set; get; }
public long latest_deposit { set; get; }
public string date_withdraw { set; get; }
public string date_deposit { set; get; }
public string date_create { set; get; }
//indexer
public string this[int i]
{
set
{
data[i] = value;
}
get
{
if (i >= 0 && i < data.Length)
return data[i];
return null;
}
}
public string this[string key]
{
get
{
return this[FindIndex(key)];
}
}
private int FindIndex(string key)
{
for (int i = 0; i < keys.Length; i++)
if (keys[i] == key)
return i;
return -1;
}
//methods
//withdraw from the account, record the current time
public void withdrawMoney()
{
Console.Write("amount(withdraw): ");
latest_withdraw = Convert.ToInt32(Console.ReadLine());
if (latest_withdraw <= total_amount)
{
total_amount -= latest_withdraw;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(latest_withdraw);
}
else
Console.WriteLine("Lack of balance. Operation is refused\n");
}
//deposit from the account, record the current time
public void depositMoney()
{
Console.Write("amount(deposit): ");
latest_deposit = Convert.ToInt32(Console.ReadLine());
if (latest_deposit > 0)
{
total_amount += latest_deposit;
this[2] = Convert.ToString(total_amount);
date_deposit = DateTime.Now.ToString();
this[4] = Convert.ToString(latest_deposit);
}
else
Console.WriteLine("Invalid operation\n");
}
//get information about the account
void get_card_info() //try 4 choices below
{
Console.WriteLine("( card ID / holder's name / total sum / latest withdraw / latest deposit )?");
string instr = Console.ReadLine();
if (instr == "card ID" || instr == "holder's name" || instr == "total sum" || instr == "latest withdraw"
|| instr == "latest deposit")
{
this[3] = Convert.ToString(latest_withdraw);
this[2] = Convert.ToString(total_amount);
Console.Write(instr + " is " + this[instr]);
if (instr == "latest withdraw")
Console.WriteLine(" " + date_withdraw);
else if (instr == "latest deposit")
Console.WriteLine(" " + date_deposit);
else if (instr == "card ID")
Console.WriteLine(" " + date_create);
else if (instr == "card ID" || instr == "total sum")
Console.WriteLine("\n");
}
else
Console.WriteLine("Invalid input!!");
}
//Inheritance, subclass CreditAccount
protected class CreditAccount : Account
{
//Constructor
CreditAccount(long bank_cardID, string password)
{
this.bank_cardID = bank_cardID;
this.account_password = password;
}
//new field
private long line_of_credit; //line of credit
//new property
public string credit_rating { set; get; }
//new method
public long get_line_of_credit() //line of credit according to the credit rating
{
if (credit_rating == "3" || credit_rating == "2")
line_of_credit = 50000;
else if (credit_rating == "1" || credit_rating == "0")
line_of_credit = 10000;
else
line_of_credit = 0;
return line_of_credit;
}
//override method withdrawMoney()
new public void withdrawMoney()
{
Console.Write("amount(withdraw): ");
latest_withdraw = Convert.ToInt32(Console.ReadLine());
if (latest_withdraw <= total_amount + line_of_credit)
{
total_amount -= latest_withdraw;
this[2] = Convert.ToString(total_amount);
date_withdraw = DateTime.Now.ToString();
this[3] = Convert.ToString(latest_withdraw);
if (latest_withdraw >= total_amount)
{
Console.WriteLine("warning: you're using your credit!! Withdraw successfully");
int temp = Convert.ToInt32(credit_rating);
credit_rating = Convert.ToString(--temp);
get_line_of_credit();
}
}
else
{
Console.WriteLine("Lack of balance. Operation is refused\n");
}
}
public static void Main(String[] args)
{
Account a;
CreditAccount ca;
string card_category;
//create a new account, set password, get an ID number
void create_account()
{
Console.WriteLine("######### " + bank_name + " #########"); //which bank
Console.Write("create an account ( normal / credit )?");
card_category = Console.ReadLine();
if (card_category != "credit" && card_category != "normal")
{
Console.WriteLine("Invalid input");
create_account();
}
Console.Write("set password: ");
string password = Console.ReadLine(); //set password
Account a_create = new CreditAccount(initial_ID, password);
a = a_create;
ca = (CreditAccount)a;
a[0] = Convert.ToString(initial_ID); //save ID
Console.Write("Your name: ");
a[1] = Console.ReadLine(); //save owner's name
a[2] = Convert.ToString(a.total_amount);
a.date_create = DateTime.Now.ToString(); //save the time that this account was created
Console.WriteLine("create successfully!!\nYour ID: " + initial_ID + " " +
"Remember your password:" + password + " You have $100000 initially.");
initial_ID++;
a.latest_deposit = 0;
a.latest_withdraw = 0;
if (card_category == "credit")
{
ca.credit_rating = "3";
ca.get_line_of_credit();
}
}
create_account();
while (true)
{
if (card_category == "normal")
{
//ask for the next instruction from the user
Console.WriteLine("( create again / get information / withdraw / deposit )?");
switch (Console.ReadLine())
{
case "create again": create_account(); break;
case "get information":
a.get_card_info(); break;
case "withdraw":
a.withdrawMoney();
a[2] = Convert.ToString(a.latest_withdraw);
break;
case "deposit":
a.depositMoney();
a[3] = Convert.ToString(a.latest_deposit);
break;
default:
Console.WriteLine("invalid input\n");
break;
}
}
else if (card_category == "credit")
{
//ask for the next instruction from the user
Console.WriteLine("( create again / get information / withdraw / deposit / line of credit )?");
switch (Console.ReadLine())
{
case "create again": create_account(); break;
case "get information":
ca.get_card_info(); break;
case "withdraw":
ca.withdrawMoney();
ca[2] = Convert.ToString(ca.latest_withdraw);
break;
case "deposit":
ca.depositMoney();
ca[3] = Convert.ToString(ca.latest_deposit);
break;
case "line of credit":
Console.WriteLine("LIne of credit: " + ca.get_line_of_credit());
break;
default:
Console.WriteLine("invalid input\n");
break;
}
}
}
}
}
}
}