调用构造方法实例化对象
public class User {
public int id;
private String name;
String address;
protected int sno;
public User ( ) { }
public User ( int id, String name, String address, int sno) {
super ( ) ;
this . id = id;
this . name = name;
this . address = address;
this . sno = sno;
}
public void study ( ) {
System. out. println ( "study..." ) ;
}
public void eat ( int a, String b) {
System. out. println ( "eat..." ) ;
}
@Override
public String toString ( ) {
return "User [id=" + id + ", name=" + name + ", address=" + address + ", sno=" + sno + "]" ;
}
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ( ( address == null) ? 0 : address. hashCode ( ) ) ;
result = prime * result + id;
result = prime * result + ( ( name == null) ? 0 : name. hashCode ( ) ) ;
result = prime * result + sno;
return result;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null)
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
User other = ( User) obj;
if ( address == null) {
if ( other. address != null)
return false ;
} else if ( ! address. equals ( other. address) )
return false ;
if ( id != other. id)
return false ;
if ( name == null) {
if ( other. name != null)
return false ;
} else if ( ! name. equals ( other. name) )
return false ;
if ( sno != other. sno)
return false ;
return true ;
}
}
import java. lang. reflect. Constructor;
public class Test {
public static void main ( String[ ] args) throws Exception {
Class userClass = Class. forName ( "com.lzj.reflect.pojo.User" ) ;
Object object = userClass. newInstance ( ) ;
System. out. println ( object) ;
Constructor constructor = userClass. getConstructor ( ) ;
Object object2 = constructor. newInstance ( ) ;
System. out. println ( object2) ;
Constructor constructor2 = userClass. getConstructor ( int . class , String. class , String. class , int . class ) ;
Object object3 = constructor2. newInstance ( 1 , "lzj" , "diqiu" , 100 ) ;
System. out. println ( object3) ;
}
}