package com.bjsxt.com;import java.util.Scanner;
public class Test{
public static void main(String []args){
Run run=new Run();
run.method1();
}
}
class People{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void getMessage(){
System.out.println("The name of people is:"+name);
}
}
class Run{
private static Run run=new Run();
private People people=new People();
private static Scanner sc=new Scanner(System.in);
public void method1(){
run.method2();
System.out.println("in method1:");
people.getMessage();
}
public void method2(){
run.method3();
}
public void method3(){
System.out.println("Please input the name of people");
people.setName(sc.nextLine());
System.out.println("in method3:");
people.getMessage();
}
}
public class Test{
public static void main(String []args){
Run run=new Run();
run.method1();
}
}
class People{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void getMessage(){
System.out.println("The name of people is:"+name);
}
}
class Run{
private static Run run=new Run();
private People people=new People();
private static Scanner sc=new Scanner(System.in);
public void method1(){
run.method2();
System.out.println("in method1:");
people.getMessage();
}
public void method2(){
run.method3();
}
public void method3(){
System.out.println("Please input the name of people");
people.setName(sc.nextLine());
System.out.println("in method3:");
people.getMessage();
}
}
static Test4 t=new Test4();//为什么加上static就能正确运行 如果把static去掉运行会报错 这是为什么啊?
public Test4(){
}
public static void main(String []args){
Test4 t1=new Test4();
}
}我加了注释的那一句谁能给我解释解释啊?
静态(static)和非静态的区别:静态是和类有关的,在类加载的时候产生,而非静态是和对象有关的,对象产生的时候非静态属性才产生,非静态方法能调用静态方法和静态属性。而静态方法只能调用静态方法和静态属性。