鱼C论坛

 找回密码
 立即注册
查看: 2772|回复: 0

[学习笔记] Java暑期学习Day13

[复制链接]
发表于 2017-7-17 23:46:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天是第13天,希望能坚持下去(同开始背cet)


                               
登录/注册后可看大图


在构造函数参数与对象数据成员同名时,可用this加以区别
  1. <div>
  2. public class CashCard {

  3.         private String number;
  4.         private int balance;
  5.         private int bonus;</div><div>
  6. public CashCard(String number,int balance,int bonus){
  7.                 this.number=number;//参数number指定给这个对象的number
  8.                 this.balance=balance;
  9.                 this.bonus=bonus;
  10.         }
  11. //...
  12. }
  13. </div>
复制代码


在Java中,this()代表了调用另一个构造函数,至于调用了哪一个构造函数,则视调用this()时给的自变量类型与个数而定。
this()调用只能出现在构造函数的第一行
不然会报          Constructor call must be the first statement in a constructor
  1. <div>public class Some {

  2.         private int a = 10;
  3.         private String text = "n.a.";

  4.         public Some(int a) {
  5.                 if (a > 0) {
  6.                         this.a = a;
  7.                 }
  8.         }

  9.         public Some(int a, String text) {

  10.                 this(a);
  11. //this(a)会调用public Some(int a)版本的构造函数,再执行if(text!=null)之后的代码程序</div><div>
  12.         if (text != null) {

  13.                         this.text = text;

  14.                 }

  15.                 // ...

  16.         }

  17. }
  18. </div>
复制代码



【报错记录】
The method main cannot be declared static; static methods can only be declared in a static or top level type
  1. package tankwar;

  2. class Other {
  3.         {
  4.                 System.out.println("对象初始区块");
  5.         }

  6.         Other() {

  7.                 System.out.println("Other()构造函数");
  8.         }

  9.         Other(int o) {
  10.                 this();
  11.                 System.out.println("Other(int o)构造函数");
  12.         }

  13. }

  14. public class Apple {
  15.         public static void main(String[] args) {
  16.                 new Other(1);
  17.         }

  18. }
复制代码

结果:

对象初始区块
Other()构造函数
Other(int o)构造函数



【报错记录】
Syntax error on token "Invalid Character", ;
expected

invalid:adj.无效的; 不能成立的; 有病的; 病人用的;
              vt.使伤残; 使退役; 失去健康;
              n.病人,病号; 残废者; 伤病军人;

  1. class Person {

  2.         private String name ;
  3.         private int age;
  4.         private String city;
  5.         public Person(){
  6.                 System.out.println("无参构造方法");
  7.         }

  8.         // 带参数的构造方法
  9.         public Person(String name, int age, String city) {
  10.                 this();
  11.                 this.name = name;
  12.                 this.age = age;
  13.                 this.city = city;

  14.         }

  15.         public void setCity(String city) {
  16.                 this.city = city;
  17.         }

  18.         public String getCity(String city) {
  19.                 return city;
  20.         }

  21.         public void setName(String name) {
  22.                 this.name = name;
  23.         }

  24.         public String getName(String name) {
  25.                 return name;
  26.         }

  27.         public void setAge(int age) {
  28.                 this.age = age;
  29.         }

  30.         public int getAge() {
  31.                 return age;
  32.         }

  33.         public String toString(){
  34.                 System.out.println(getAge());
  35.              return  "名字:"+this.name+",今年:"+this.age+"岁,家住:"+this.city ;
  36.      }

  37. }

  38. public class ConstructorDemo {
  39.         public static void main(String[] args) {

  40.                 Person p = new Person("张三", 18, "武汉");
  41.                 /*
  42.                  * 在堆中开辟空间,给属性分配默认的初始值 假设属性一开始赋值了,就进行赋值工作 调用构造方法来对属性进行初始化
  43.                  * p.setName("张三"); p.setAge(10);
  44.                  */

  45.                 System.out.println(p.toString());
  46.                 /*
  47.                  * Object类中的toString()方法源码:
  48. public String toString() {
  49. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  50. }
  51. 假如类复写了Object类(此类为Java根基类)中的toString()方法 例如:
  52. public String toString(){
  53. return "Hello"; //这里才是要返回的值 如果没复写 则调用Object类中的toString()方法(打印类的全限命名+内存地址) }
  54.                  *
  55.                  */
  56.         }
  57. }
复制代码






评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 14:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表