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

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);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,924评论 18 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,891评论 0 23
  • 人工智能,一个越来越火热的名词,包括但不限于BAT的互联网巨头都对其趋之若鹜。大量资金的注入,人才的培养,战略的制...
    empror阅读 284评论 0 0
  • 一早就收到了大姑子送的康乃馨,幸福! 十点多儿子发信息祝母亲节快乐! 用布衣老师教的心法抄一遍对话内容。...
    墨蘭阅读 164评论 1 1