zoukankan      html  css  js  c++  java
  • 构造方法的主要应用

    Book类:代码
     1 package org.hanqi.pn0120;
     2 
     3 public class Book {
     4     
     5     //构造方法:  
     6     //1.构造方法必须与类名相同;
     7     //2.没有返回值,不需要加void;
     8     //3.只要自定义了构造方法,就不会自动添加默认构造方法。
     9     Book()
    10     {
    11         //完成初始化
    12         name="默认书名";
    13     }
    14     
    15     Book(String bookname)
    16     {
    17         //初始化
    18         name=bookname;
    19     }
    20     Book(String bookname,String category)
    21     {
    22         name=bookname;
    23         //this 代表类本身
    24         this.category=category;
    25     }
    26     
    27     //属性     成员变量
    28     String name;//书名
    29     String category="其他类";//书的类别
    30     String content="";//书的内容
    31     double price=10;//书的价格
    32     //行为     成员方法
    33     void print()//void 代表没有返回值    括号中为空表示没有传入参数
    34     {
    35         System.out.println("我读的这本书书名为:《"+name+"》");
    36         System.out.println("我读书的内容为:"+content);
    37     }
    38     //形式之二:有参数无返回值
    39     void write(String mycontent)//content:内容的意思
    40     {
    41         System.out.println("我写的内容:"+mycontent);
    42         if(content.length()>=1000)
    43         {
    44             return;
    45         }
    46         content+=mycontent;
    47         System.out.println("我写完之后的内容:"+content);
    48     }
    49     //第三种形式: 有返回值,没有传入参数
    50     String read()
    51     {
    52         //返回值
    53         return content;
    54     }
    55     //第四种形式:有返回值,有传入参数
    56     //卖书    根据卖书的数量计算并返回钱数
    57     double sell(int count)//count:总数
    58     {
    59         return(price*count);
    60     }
    61     
    62     
    63 }
    View Code

    Test类代码:

     1 package org.hanqi.pn0120;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         //构造一本书(构造书类中的一个新的对象)
     8         Book myBook=new Book("JAVA基础教程","JAVA教程类");//默认构造方法
     9         //myBook.name="JAVA基础教程";
    10         System.out.println("类别="+myBook.category);
    11         myBook.category="教程类";
    12         System.out.println("类别="+myBook.category);
    13         
    14         myBook.content="第一章 ";
    15         myBook.read();
    16         String str=myBook.read();//将返回的赋给str
    17         System.out.println("读出的内容="+str);
    18         myBook.write("JAVA的发展史");
    19         myBook.read();
    20         myBook.print();
    21         str=myBook.read();
    22         System.out.println("读出的内容="+str);
    23         myBook.sell(3);//卖了三本书
    24         int c=3;//卖出的本数
    25         System.out.println("卖"+c+"书的钱数"+myBook.sell(3));
    26         Book myBook1=new Book("战争与和平");//有参数的初始化方法
    27         //myBook1.name="战争与和平";
    28         myBook1.category="名著类";
    29 //        myBook1.content="第一篇 ";
    30 //        myBook1.write("战争    和平   ");
    31         myBook1.read();
    32         myBook1.print();
    33         
    34         
    35         
    36         
    37 
    38     }
    39 
    40 }
    View Code

    运行结果:

  • 相关阅读:
    NOI2005 聪聪和可可
    CodeVS 1344 线型网络
    BZOJ 2466: [中山市选2009]树
    BZOJ 3827: [Poi2014]Around the world
    BZOJ 1109: [POI2007]堆积木Klo
    BZOJ 2124: 等差子序列
    BZOJ 4544: 椭圆上的整点
    BZOJ 2342: [Shoi2011]双倍回文
    BZOJ 2084: [Poi2010]Antisymmetry
    BZOJ 3111: [Zjoi2013]蚂蚁寻路
  • 原文地址:https://www.cnblogs.com/beens/p/5242658.html
Copyright © 2011-2022 走看看