1.1用类制造对象
1.2定义类
这里我们定义了一个类
这里我们写一个VendingMachine的类
package vendingmachine;
public class VendingMachine {
int price = 80;
int balance;
int total;
void showPrompt()
{
System.out.println("Welcome");
}
void insertMoney(int amount) {
balance = balance + amount;
}
void showBalance() {
System.out.println(balance);
}
void getFood() {
if(balance >= price) {
System.out.println("Here you are!");
balance = balance - price;
total = total + price;
}
}
public static void main(String[] args) {
VendingMachine vm = new VendingMachine();
vm.showPrompt();
vm.showBalance();
vm.insertMoney(100);
vm.getFood();
vm.showBalance();
VendingMachine vm1 = new VendingMachine();
vm1.insertMoney(200);
vm.showBalance();
vm1.showBalance();
}
}
运行结果
Welcome
0
Here you are!
20
20
200
1.3 成员变量和成员函数
关于this的使用,在刚才那个程序的基础上,我们修改了一下,注意当中的showPrice()和setPrice()
package vendingmachine;
public class VendingMachine {
int price = 80;
int balance;
int total;
void setPrice(int price) {
this.price = price;
}
void showPrice() {
System.out.println(this.price);
}
void showPrompt()
{
System.out.println("Welcome");
}
void insertMoney(int amount) {
balance = balance + amount;
}
void showBalance() {
System.out.println(this.balance);
}
void getFood() {
if(balance >= price) {
System.out.println("Here you are!");
balance = balance - price;
total = total + price;
}
}
public static void main(String[] args) {
VendingMachine vm = new VendingMachine();
vm.showPrice();
vm.setPrice(55);
vm.showPrice();
}
}
1.4对象初始化
package vendingmachine;
public class VendingMachine {
int price = 80;
int balance;
int total;
//构造函数
VendingMachine(){
total = 0;
}
//构造函数
//重载
VendingMachine(int price) {
this.price = price;
}
void setPrice(int price) {
this.price = price;
}
void showPrice() {
System.out.println(this.price);
}
void showPrompt()
{
System.out.println("Welcome");
}
void insertMoney(int amount) {
balance = balance + amount;
}
void showBalance() {
System.out.println(this.balance);
}
void getFood() {
if(balance >= price) {
System.out.println("Here you are!");
balance = balance - price;
total = total + price;
}
}
public static void main(String[] args) {
VendingMachine vm = new VendingMachine();
vm.showPrice();
vm.setPrice(55);
vm.showPrice();
VendingMachine vm1 = new VendingMachine(100);
vm1.showPrice();
}
}
1.5 本章练习
package week1;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Fraction a = new Fraction(in.nextInt(), in.nextInt());
Fraction b = new Fraction(in.nextInt(),in.nextInt());
a.print();
b.print();
a.plus(b).print();
a.multiply(b).plus(new Fraction(5,6)).print();
a.print();
b.print();
in.close();
}
}
class Fraction {
int up;
int down;
Fraction(int a,int b){
this.up = a;
this.down = b;
}
double toDouble() {
return (double)(this.up/this.down);
}
Fraction plus(Fraction o){
boolean found = false;
int num=this.down*o.down;
int up_new = o.down*this.up +this.down*o.up;
Fraction fra = new Fraction(up_new,num);
return fra;
}
Fraction multiply(Fraction o){
int up_new = this.up * o.up;
int down_new = this.down * o.down;
Fraction fra = new Fraction(up_new,down_new);
return fra;
}
void print() {
if(this.up%this.down ==0) {
System.out.println(this.up/this.down);
}
//这里的一个问题是要最简化分式,然后输出
//自然想到辗转相除法
//参考
//int GCD(int a,int b)
//{
// return b==0?a:GCD(b,a%b);
//}
else {
int a = this.up;
int b = this.down;
int c = 0;
while(b!=0) {
c = b;
b = a%b;
a = c;
}
System.out.println(this.up/a+"/"+this.down/a);
}
}
}