华恩JAVA班第15天
作业:
1.创建一个没有构造方法的类,并在main()中创建对象,用以验证编译器是否真的加入了默认构造方法。
public class Test
{
public static void main(String[] args)
{
Student stu = newStudent();
}
}
class Student{
String name;
int age;
}
2.编写两个具有(重载)构造方法的类,并在第一个构造方法中通过this调用第二个构造方法。
public class Test
{
public static void main(String[] args)
{
Student stu1 = newStudent("王五");
Student stu2 = newStudent("李四",13);
System.out.println(stu1.name);
System.out.println(stu2.name+"\t"+stu2.age);
}
}
class Student{
String name;
int age;
Student(String name){
this.name = name;
}
Student(String name,int age){
this(name);
this.age = age;
}
}
static关键字
public class Test
{
public static void main(String[] args)
{
Student A = newStudent();
//A.print();
Student.print();
A.p();
A.country = "中国";
Student B = newStudent();
//System.out.println(B.country);
System.out.println(Student.country);
}
}
class Student
{
String name;
intage; //实例变量
static String country;//静态变量(类变量)
static void print(){
//System.out.println(name); 错误: 无法从静态上下文中引用非静态 变量 name
System.out.println("*"+country);
String AA = "你好世界";
System.out.println(AA);
}
void p(){
System.out.println("&"+name);
System.out.println("!"+country);
}
}