吉大《java程序设计》复习资料(三)
吉大《JAVA程序设计》拓展资源(三)第三章 面向对象(上)
内部类如何被外部引用
class Outer
{ private int size=10;
public class Inner //将内部类声明为public
{ public void doStuff()
{
System.out.println(++size);
}
}
}
public class TestInner
{ public static void main( String[] args)
{
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.doStuff();
}
}
为软件复用创建包
package com.deitel.jhtp4.ch08;
import java.text.DecimalFormat;
public class Time1 extends Object
{private int hour; // 0-23 private int minute; // 0-59 private int second; // 0-59
public Time1() { setTime( 0, 0, 0 ); }
public void setTime( int h, int m, int s )
{ hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
second = ( ( s >= 0 && s < 60 ) ? s : 0 ); }
public String toUniversalString()
{ DecimalFormat twoDigits = new DecimalFormat( "00" );
return twoDigits.format( hour ) + ":" +twoDigits.format( minute ) + ":" +
twoDigits.format( second ); }
public String toString()
{ DecimalFormat twoDigits = new DecimalFormat( "00" );
return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +
":" + twoDigits.format( minute ) + ":" +
twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" );
}
}//
import javax.swing.JOptionPane;
import com.deitel.jhtp4.ch08.Time1;// import Time1 class
public class TimeTest3
{
public static void main( String args[] )
{ Time1 time = new Time1(); // create Time1 object
time.setTime( 13, 27, 06 );// set new time
String output = "Universal time is: " + time.toUniversalString()
+ "\nStandard time is: " + time.toString();
JOptionPane.showMessageDialog( null, output,
"Packaging Class Time1 for Reuse",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}奥鹏作业答案可以联系QQ 761296021
页:
[1]