首页 > 其他分享 >Class实例对应着运行时类

Class实例对应着运行时类

时间:2022-11-30 17:59:12浏览次数:43  
标签:name age Class Person 实例 时类 public String

package day2;

import org.junit.Test;

public class ClassDemo1 {
//程序经过javac.exe生成.class字节码文件
//用java.exe解释运行某个字节码文件,相当于将其加载到内存中,此过程为类的加载,该类为运行时类
//Class实例对应着运行时类
@Test
public void test1() throws Exception {
Person p1=new Person("张三",20);
//3种方式获取Class实例
Class c1=Person.class;
System.out.println(c1);
Class c2=p1.getClass();
System.out.println(c2);
//通过类的路径
Class c3=Class.forName("day2.Person");
System.out.println(c3);
}
}

class Person {
private String name;
public int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}
private Person(String name) {
    this.name = name;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
@Override
public String toString() {
    return "姓名:"+ name + ",年龄:" + age;
}
public String show(){
    return this.name+" , "+this.age;
}
private String showNation(String nation){
    return "我来自"+nation;
}

}

标签:name,age,Class,Person,实例,时类,public,String
From: https://www.cnblogs.com/fighterk/p/16939247.html

相关文章