62,614
社区成员
发帖
与我相关
我的任务
分享
class Person{
String name;
int age;
Person(){
name="noname";
age=1;
}
Person(String name){
this();
this.name=name;
}
Person(int age){
this();
this.age=age;
}
Person(String name,int age){
this.name=name;
this.age=age;
}
void print(){
System.out.println ("我的名字是"+name+",年龄是"+age);
}
}
class Test{
public static void main(String [ ] args){
Person p1=new Person();
p1.print();
Person p2=new Person("小明");
p2.print();
Person p3=new Person(10);
p3.print();
Person p4=new Person("小芳",11);
p4.print();
}
}
class Person{
String name="noname";
int age=1; //这里变成初始化
/*
Person(){
name="noname";
age=1;
}*/
Person(String name){
this(); //运行时这里就开始报错,说找不到符号,构造函数Person()
this.name=name;
}
...........
class Person{
String name="noname";
int age=1; //这里变成初始化
Person(){
//name="noname";
//age=1;
}
Person(String name){
this();
this.name=name;
}
...........
// Time3.java类的定义
public class Time3 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// Time3 constructor initializes each instance variable
// to zero. Ensures that Time object starts in a
// consistent state.
public Time3()
{
this.setTime( 0, 0, 0 ); //这个this指向何方,如果上面教程的话说this指向第一个构造函数,但是这个就是this就是本身了public Time3(){}了
}
// Time3 constructor: hour supplied, minute and second
// defaulted to 0
public Time3( int h )
{
this.setTime( h, 0, 0 );
}
// Time3 constructor: hour and minute supplied, second
// defaulted to 0
public Time3( int h, int m )
{
this.setTime( h, m, 0 );
}
public Time3( int h, int m, int s )
{
this.setTime( h, m, s );
}
// Time3 constructor: another Time3 object supplied
public Time3( Time3 time )
{
this.setTime( time.getHour(), time.getMinute(),
time.getSecond() );
}
// Set Methods
// set a new Time value using universal time
public Time3 setTime( int hour, int minute, int second )
{
this.setHour( hour ); // set hour
this.setMinute( minute ); // set minute
this.setSecond( second ); // set second
return this; // enables chaining
}
// validate and set hour
public Time3 setHour( int hour )
{
this.hour = ( hour >= 0 && hour < 24 ? hour : 0 );
return this; // enables chaining
}
public Time3 setMinute( int minute )
{
this.minute = ( minute >= 0 && minute < 60 ) ? minute : 0;
return this; // enables chaining
}
// validate and set second
public Time3 setSecond( int second )
{
this.second = ( second >= 0 && second < 60 ) ? second : 0;
return this; // enables chaining
}
// get value of hour
public int getHour() { return this.hour; }
// get value of minute
public int getMinute() { return this.minute; }
// get value of second
public int getSecond() { return this.second; }
// convert to String in universal-time format
public String toUniversalString()
{
return hour + ":" + minute + ":" + second ;
}
// convert to String in standard-time format
public String toString()
{
return ((hour == 12 || hour == 0) ? 12 : hour % 12 ) +":" + minute+":" + second + ( hour < 12 ? " AM" : " PM" );
}
public static void main( String args[] )
{
Time3 t1, t2, t3, t4, t5, t6;
t1 = new Time3(); // 00:00:00
t2 = new Time3( 2 ); // 02:00:00
t3 = new Time3( 21, 34 ); // 21:34:00
t4 = new Time3( 12, 25, 42 ); // 12:25:42
t5 = new Time3( 27, 74, 99 ); // 00:00:00
t6 = new Time3( t4 ); // 12:25:42
System.out.println(t1.toUniversalString()+'\t'+ t1.toString());
System.out.println(t2.toUniversalString()+'\t'+ t2.toString());
System.out.println(t3.toUniversalString()+'\t'+ t3.toString());
System.out.println(t4.toUniversalString()+'\t'+ t4.toString());
System.out.println(t5.toUniversalString()+'\t'+ t5.toString());
System.out.println(t6.toUniversalString()+'\t'+ t6.toString());
}
}
public class Test
{
private int id;
private String name;
public Test()
{
this.name = "";//表示本对象中的name成员变量
}
public Test(int i)
{
this();//表示无参构造函数Test()
this.id = i;//表示本对象中的id成员变量
}
public Test(int i, String name)
{
this(i);//表示有一个参构的造函数Test(int i)
}
}
public class Test{
private int a=100;
public int getA(){
return this.a;//这里的this就是指当类对象的.a就是调用当前类对象中的a属性
}
public getB(){
this.getA();//这个里.getA()就是调用当前类对象中的getA()方法·
}
}