美文网首页
面向对象 --初体验(类,方法,对象,构造函数)

面向对象 --初体验(类,方法,对象,构造函数)

作者: x曦月y | 来源:发表于2016-07-21 13:57 被阅读5次

1.类,方法,对象,构造函数的例题(1)

public class Account {

//private 只在本类中运行。

//public在所有的工程中都可以调用。

//protected只在本类本包和子类中调用。    //实例方法如果是public类型则所有可运行。

//default在本来和子类中被调用。

//get有返回值但是没有参数,set没有返回值但是有参数。

private String id;

private String name;

private int balance = 0;

public Account(String id, String name) {

this.id = id;

this.name = name;

}

public Account(String id, String name, int balance) {

this.id = id;

this.name = name;

this.balance = balance;

}

public String getID() {

return id;

}

public String getName() {

return name;

}

public int getbalance() {

return balance;

}

public int credit(int amount) {

balance=balance+amount;

return balance;

}

public int debit(int amount) {

if (amount <= balance) {

balance=balance -amount;

} else {

System.out.println("Amount exceeded balance");

}

return balance;

}

public int transferto(Account another, int amount) {

if (amount <= balance) {

balance =balance-amount;

another.credit(amount);

} else {

System.out.println("Amount exceeded balance");

}

return balance;

}

public String toString() {

return "Account=[id=" + id + ",name=" + name + ",balance=" + balance + ",]";

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Account ac = new Account("2014", "xy",5000); // new是一个关键字,new 的时候就调用了构造函数

ac.credit(5000);

System.out.println(ac.toString()); // 调用函数时,格式要一致。看调用的方法是否有参数,有则需传入对应的参数类型。

}//Account=[id=2014,name=xy,balance=10000,]

}

1.类,方法,对象,构造函数的例题(2)//类的包含。

public class Book{

private String name;

private Author author;

private double price;

private int qty=0;

public Book(String name,Author author,double price){

this.name=name;

this.author=author; //通过构造函数构造了一个实例对象,故在后面再本来中可以直接调用

this.price=price;

}

public Book(String name,Author author,double price,int qty){

this.name=name;

this.author=author;

this.qty=qty;

}

public String getName(){

return name;

}

public Author getAuthor(){

return author;

}

public double getPrice(){

return qty;

}

public void setPrice(double price){

this.price=price;

}

public int getQty(){

return qty;

}

public void setQty(int qty){

this.qty=qty;

}

public String toString(){

String i="Book{name="+name+",email="+author.getEmail()+",gender="+author.getGender()+",price="+price+",qty="+qty+"]";

return i;

}

}


public class Book{

private String name;

private Author author;

private double price;

private int qty=0;

public Book(String name,Author author,double price){

this.name=name;

this.author=author;

this.price=price;

}

public Book(String name,Author author,double price,int qty){

this.name=name;

this.author=author;

this.qty=qty;

}

public String getName(){

return name;

}

public Author getAuthor(){

return author;

}

public double getPrice(){

return qty;

}

public void setPrice(double price){

this.price=price;

}

public int getQty(){

return qty;

}

public void setQty(int qty){

this.qty=qty;

}

public String toString(){

String i="Book{name="+name+",email="+author.getEmail()+",gender="+author.getGender()+",price="+price+",qty="+qty+"]";

return i;}}


public class Book_Test {

public static void main(String[] args) {

Author au = new Author(); //在输出之前需要实例化Author,才能调用。

Book bk = new Book("xiaoyang",au,2000);

System.out.println(bk.toString());}}


1.类,方法,对象,构造函数的例题(3)--将author变成数组。

authors:Author[];

Book[name=xy,author={Author[name=聪哥,email=1@qq.com,gender=女],Author[name=周嘉宇,email=113@qq.com,gender=男]},price=2000.0,qty=0] //输出结果

Book[name=xy,author={},price=2000.0,qty=0] //book.toString()中输出的内容。

Author[name=周嘉宇,email=113@qq.com,gender=男] //Author.toString()中输出的内容。

main 方法中:

Author[] au = {new Author("李聪聪","1015612131@qq.com",'女'),new Author("周嘉宇","11351313@qq.com",'男')}; ——>Author[] au = {new Book(), new Author() };

Book bk = new Book("xiaoyang",au,2000);

相关文章

网友评论

      本文标题:面向对象 --初体验(类,方法,对象,构造函数)

      本文链接:https://www.haomeiwen.com/subject/fjosjttx.html