为什么没报错?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

5回答
没有马甲线的安琪拉 2017-11-29 14:48:32
1
2
3
 //构造方法
 public Z_Cat() {}
 static int temp = 14;

大哥,您老这个static语句时写在方法外面,直接写类里当然没问题啊。。。。

  • 提问者 OrcaIMOOC #1
    【捂脸】,感谢感谢
    2017-11-29 15:03:02
提问者 OrcaIMOOC 2017-11-29 14:41:35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Z_Cat_T {
 
 public static void main(String[] args) {
 
  //对象实例化
 
  //对象:猫1;
 
  Z_Cat one = new Z_Cat(); 
 
  //猫1,test赋值:姓名、年龄、体重、品种、价格
 
  one.setName("波子");
 
  one.setAge(3);//-3
 
  one.setWeight(23);
 
  one.setSpecies("英短");
 
  //对象:猫2;
 
  Z_Cat two = new Z_Cat();
 
  //猫2,test赋值:同上
 
  two.setName("阿龙");
 
  two.setAge(2);
 
  two.setWeight(20);
 
  two.setSpecies("中华田园");
 
  //输出属性
 
  System.out.println("呢称:" + one.getName());
 
  if(one.getAge()==0)
 
   return;
 
  System.out.println("年龄:" + one.getAge());
 
  System.out.println("体重:" + one.getWeight());
 
  System.out.println("品种:" + one.getSpecies());
 
  //输出方法
 
  one.run(one.getName());
 
  one.eat(one.getName()); 
 
 }

提问者 OrcaIMOOC 2017-11-29 14:40:16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class Z_Cat {
 //属性:隐藏属性可见性 —— private
 private String name; //隐藏后,实例化测试页不能访问。限定当前类内访问
 private int age;
 private double weight;
 private String species;
  
 //构造方法
 public Z_Cat() {}
  static int temp = 14;
 public Z_Cat(int age) {
  //this.age = age;
  this.setAge(age);
 }
  
 //公有方法:get/set
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
  
 public int getAge() { 
  return age;
 }
 public void setAge(int age) {
  if(age<=0)
   System.out.println("输入年龄错误,必须大于0");
  else
   this.age = age;
 }
 public double getWeight() {
  return weight;
 }
 public void setWeight(double weight) {
  this.weight = weight;
 }
 public String getSpecies() {
  return species;
 }
 public void setSpecies(String species) {
  this.species = species;
 }
  
 //成员方法
 //方法:错误提示。
 public void mistake() {
  System.out.println("输入信息错误");
 }
  
 //方法:跑
 public  void run() {
  System.out.println("跑");
 }
 public void run(String name){
  this.name = name;
  System.out.println(name + "跑");
 }
  
 //方法:吃
 public void eat() {
  System.out.println("吃");
 }
 public void eat(String name) {
  this.name = name;
  System.out.println(name + "吃");
 }
}

 

没有马甲线的安琪拉 2017-11-28 18:02:09

我的就会报错啊,要不把你的源码全部贴出来看看

  • 提问者 OrcaIMOOC #1
    public class Z_Cat { //属性:隐藏属性可见性 —— private private String name; //隐藏后,实例化测试页不能访问。限定当前类内访问 private int age; private double weight; private String species; //构造方法 public Z_Cat() {} static int temp = 14; public Z_Cat(int age) { //this.age = age; this.setAge(age); } //公有方法:get/set public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age<=0) System.out.println("输入年龄错误,必须大于0"); else this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } //成员方法 //方法:错误提示。 public void mistake() { System.out.println("输入信息错误"); } //方法:跑 public void run() { System.out.println("跑"); } public void run(String name){ this.name = name; System.out.println(name + "跑"); } //方法:吃 public void eat() { System.out.println("吃"); } public void eat(String name) { this.name = name; System.out.println(name + "吃"); } }
    2017-11-29 14:38:53
  • 提问者 OrcaIMOOC #2
    没想到贴在这里会成为一团~
    2017-11-29 14:39:47
没有马甲线的安琪拉 2017-11-28 17:04:05

你的jdk版本是多少

  • 提问者 OrcaIMOOC #1
    没记错就是8.1
    2017-11-28 17:31:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码