华恩JAVA班第15天

华恩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);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值