c++改java需要注意以下几个方面:
(1)Java里面没有namespace,有package。而且Java里不用写头文件。
(2)private,public等在Java里面需要单独写在每一个成员前面。
private: //原代码节选 int id; //账号 double balance; //余额 double rate; //存款的年利率 int lastDate; //上次变更余额的时期 double accumulation; //余额按日累加之和
//改写后 private int id; //账号 private double balance; //余额 private double rate; //存款的年利率 private int lastDate; //上次变更余额的时期 private double accumulation; //余额按日累加之和
3)Java里不允许普通函数的声明与函数体分开.
如:
private void record(int date, double amount)
private double accumulate(int date)
(4)Java里没有const,但是有final,但是功能有差距
(5)Java里的输出是语句是System.out.println(变量名 + “字符串”);
public void show() { System.out.println("#"+id+"\tBalance: "+balance); }
(6)c++ 里面建立类的对象可以直接:类名 对象名(参数),而Java里面需要:类名 对象名 = new 类名(参数)。
(7)Java里的构造函数大多是this.id=id这种写法。
(8)c++调用类的静态成员函数是 类名::函数名,而Java是 类名.函数名。
(9)Java里没有像c++一样的&符号引用,创建对象时一律使用new。
(10)Java里没有全局变量。
(11)Java里的异常退出exit(1)需要调用System.exit(1)。
(12)Java里的字符串类型是一个类String,还有print不输出换行,println才换行。
下面是个人银行账户管理程序c++源码4_9的C++源码以及改正过后的JAVA代码:
c++源码
//4_9.cpp #include <iostream> #include <cmath> using namespace std; class SavingsAccount { //储蓄账户类 private: int id; //账号 double balance; //余额 double rate; //存款的年利率 int lastDate; //上次变更余额的时期 double accumulation; //余额按日累加之和 //记录一笔帐,date为日期,amount为金额,desc为说明 void record(int date, double amount); //获得到指定日期为止的存款金额按日累积值 double accumulate(int date) const { return accumulation + balance * (date - lastDate); } public: //构造函数 SavingsAccount(int date, int id, double rate); int getId() { return id; } double getBalance() { return balance; } double getRate() { return rate; } //存入现金 void deposit(int date, double amount); //取出现金 void withdraw(int date, double amount); //结算利息,每年1月1日调用一次该函数 void settle(int date); //显示账户信息 void show(); }; //SavingsAccount类相关成员函数的实现 SavingsAccount::SavingsAccount(int date, int id, double rate) : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { cout << date << "\t#" << id << " is created" << endl; } void SavingsAccount::record(int date, double amount) { accumulation = accumulate(date); lastDate = date; amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 balance += amount; cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; } void SavingsAccount::deposit(int date, double amount) { record(date, amount); } void SavingsAccount::withdraw(int date, double amount) { if (amount > getBalance()) cout << "Error: not enough money" << endl; else record(date, -amount); } void SavingsAccount::settle(int date) { double interest = accumulate(date) * rate / 365; //计算年息 if (interest != 0) record(date, interest); accumulation = 0; } void SavingsAccount::show() { cout << "#" << id << "\tBalance: " << balance; } int main() { //建立几个账户 SavingsAccount sa0(1, 21325302, 0.015); SavingsAccount sa1(1, 58320212, 0.015); //几笔账目 sa0.deposit(5, 5000); sa1.deposit(25, 10000); sa0.deposit(45, 5500); sa1.withdraw(60, 4000); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个账户信息 sa0.show(); cout << endl; sa1.show(); cout << endl; return 0; }
改成的Java代码:
class SavingsAccount{ //储蓄账户 private int id; //账号 private double balance; //余额 private double rate; //存款的年利率 private int lastDate; //上次变更余额的时期 private double accumulation; //余额按日累加之和 //记录一笔帐,date为日期,amount为金额,desc为说明 private void record(int date, double amount){ accumulation = accumulate(date); lastDate = date; amount = Math.floor(amount * 100 + 0.5) / 100; //保留小数点后两位 balance += amount; System.out.println( date +"\\t#"+id+"\t" + amount +"\t"+balance ); } //获得到指定日期为止的存款金额按日累积值 private double accumulate(int date) { return accumulation + balance * (date - lastDate); } public SavingsAccount (int date, int id, double rate) { this.id=id; balance=0; this.rate=rate; lastDate=date; accumulation=0; System.out.println(date + "\t#" + id + " is created"); } int getId() { return id; } double getBalance() { return balance; } double getRate() { return rate; } //存入现金 void deposit(int date, double amount) {record(date, amount);} //取出现金 void withdraw(int date, double amount) { if (amount > getBalance()) System.out.println("Error: not enough money"); else record(date, -amount);} //结算利息,每年1月1日调用一次该函数 void settle(int date) {double interest = accumulate(date) * rate / 365; //计算年息 if (interest != 0) record(date, interest); accumulation = 0;} //显示账户信息 void show(){ System.out.println("#"+id+"\tBalance: "+balance); } }; public class a4_9 { public static void main(String[] args) { //建立几个账户 SavingsAccount sa0=new SavingsAccount (1, 21325302, 0.015); SavingsAccount sa1=new SavingsAccount (1, 58320212, 0.015); //几笔账目 sa0.deposit(5, 5000); sa1.deposit(25, 10000); sa0.deposit(45, 5500); sa1.withdraw(60, 4000); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个账户信息 sa0.show(); System.out.println("\n"); sa1.show(); System.out.println("\n"); } }
个人银行账户管理程序c++源码还有5_11,6_25,7_10,8_8,9_16等五个方面没有进行c++源码改java源码,博主正在持续更新中