在代码的最下面行有我数值的输入,Telephone phone2 = new Telephone(5.0f,1.4f,4f);
就是因为不太懂老师说的那个到底是啥意思,跟System.out.println()里面到底有没有值到底是啥关系呢?老师也一直强调这个是“有参数的构造方法,可以对变量进行赋值。”所以希望java粉们给予解答~
//Telephone.java
package com.project;
public class Telephone {
float screen;
float cpu;
float memory;
/*
public Telephone(){
System.out.println("无参的构造方法!");
}*/
public Telephone(float newScreen,float newCpu,float newMemory ){
if(newScreen<3.5f){
System.out.println("您输入的参数错误,自动赋值为3.5f");
newScreen=3.5f;
}else{
screen=newScreen;
}
screen=newScreen;
cpu=newCpu;
memory=newMemory;
System.out.println();
}
}
//initialTelephone.java
package com.project;
public class initialTelephone {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*//通过无参的构造方法创造对象
Telephone phone = new Telephone();
*/
//通过有参的构造方法创造对象,并对对象中的实例变量赋初值
Telephone phone2 = new Telephone(5.0f,1.4f,4f);
}
}
米沙之国
慕粉3598667
晟弡
yanrun
晓健
相关分类