public class HelloWorld {
// 静态变量score1
static int score1 = 86;
// 静态变量score2
static int score2 = 92;
// 定义静态方法sum,计算,并返回总分
public static int sum() { return score1 + score2;}
public static void main(String[] args) {
// 调用静态方法sum并接收返回值
int allScore = sum();
System.out.println("总分:" + allScore);}}
// 静态变量score1
static int score1 = 86;
// 静态变量score2
static int score2 = 92;
// 定义静态方法sum,计算,并返回总分
public static int sum() { return score1 + score2;}
public static void main(String[] args) {
// 调用静态方法sum并接收返回值
int allScore = sum();
System.out.println("总分:" + allScore);}}
2016-12-26
//定义父类
public class Vehicle(){
//父类属性
int 车厢;
int 发动机;
//父类方法
public void drive(){
输出(可以开的,载人去其他地方的东西);
}
定义子类
public class Car extends Vehicle(){
//定义子类属性
//定义子类方法
}
public class Vehicle(){
//父类属性
int 车厢;
int 发动机;
//父类方法
public void drive(){
输出(可以开的,载人去其他地方的东西);
}
定义子类
public class Car extends Vehicle(){
//定义子类属性
//定义子类方法
}
2016-12-25
至于构造方法非要用HelloWorld:在类的声明中,可以包含多个初始化块,当创建类的实例时(就是下面new的时候),就会依次执行这些代码块,所以上面的变量就被成功赋值。这种构造方法属于初始化成员变量的方法,上面的构造方法中可以是这样(public可以省略)HelloWorld () {
System.out.println("通过构造方法初始化name");
name = "tom";
}
也可以直接是 {
System.out.println("通过构造方法初始化name");
name = "tom";
}
其语法就是:修饰词 类名(参数){
}
System.out.println("通过构造方法初始化name");
name = "tom";
}
也可以直接是 {
System.out.println("通过构造方法初始化name");
name = "tom";
}
其语法就是:修饰词 类名(参数){
}
2016-12-25
至于构造方法非要用HelloWorld,和想要hello.HelloWorld的童鞋可以看看这个
public class Test {
// 构造方法
public void World () {
}
// 初始化块同上字数限制省略
public void HelloWorld() {
同上省略(字数限制)
// 创建对象
Test hello = new Test();
// 调用对象的HelloWorld方法
hello.World();
hello.HelloWorld();
}
}
public class Test {
// 构造方法
public void World () {
}
// 初始化块同上字数限制省略
public void HelloWorld() {
同上省略(字数限制)
// 创建对象
Test hello = new Test();
// 调用对象的HelloWorld方法
hello.World();
hello.HelloWorld();
}
}
2016-12-25
帮我看看哪里错了
String getState(int time){
String state=null;
if(time>=0&&24>=time){
if(time>=8&&time<17)
state="我在工作";
else if(time>17&&time<22)
state="我在学习";
else if(time>22||time<7)
state="我在睡觉";
else
state="时间错误";
return state;
}
}
String getState(int time){
String state=null;
if(time>=0&&24>=time){
if(time>=8&&time<17)
state="我在工作";
else if(time>17&&time<22)
state="我在学习";
else if(time>22||time<7)
state="我在睡觉";
else
state="时间错误";
return state;
}
}
2016-12-25
public class Male{
//define type
int JJ;
//define method
void use{
System.out.println("to make female pregnant");
}
//define type
int JJ;
//define method
void use{
System.out.println("to make female pregnant");
}
2016-12-24
方法重写: 返回值类型要相同 父类double 子类double 说可以强制转换的,那是在子类的return 返回值这个地 方 对返回值做 return (int)x; 可以返回个整型数据,也即返回值可以是个整型数据。但前面的 返回值类型必须跟父类一样,视频老师没说错。
2016-12-23
方法重写:访问修饰符可以修改,不得低于父类的可见性,但返回值类型,@Jerryqueue 父类都double 子类 int 是错的,不能强制转换,我测试了
2016-12-23