core java中的程序,讨论内部类。import java.awt.event.*;
import java.text.*;
import javax.swing.*;public class InnerClassTest
{
public static void main(String[] args)
{
// construct a bank account with initial balance of $10,000
BankAccount account = new BankAccount(10000);
// start accumulating interest at 10%
account.start(10); // keep program running until user selects "Ok"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}class BankAccount
{
/**
Constructs a bank account with an initial balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
} /**
Starts a simulation in which interest is added once per
second
@param rate the interest rate in percent
*/
public void start(double rate)
{
ActionListener adder = new InterestAdder(rate);
Timer t = new Timer(1000, adder);
t.start();
} private double balance; /**
This class adds the interest to the bank account.
The actionPerformed method is called by the timer.
*/
private class InterestAdder implements ActionListener
{
public InterestAdder(double aRate)
{
rate = aRate;
} public void actionPerformed(ActionEvent event)
{
// update interest
double interest = balance * rate / 100;
balance += interest; // print out current balance
NumberFormat formatter
= NumberFormat.getCurrencyInstance();
System.out.println("balance="
+ formatter.format(balance));
} private double rate;
}
}我的问题是:
1,ActionListener adder = new InterestAdder(rate);这条语句中ActionListener是一个接口,InterestAdder是一个实现了这个接口的类,这种定义形式有什么作用,体现了java的什么特性,照我的理解定义为InterestAdder adder = new InterestAdder(rate);也可以呀。
2,InterestAdder是BankAccount的内部类,产生了InterestAdder对象后会不会产生一个BankAccount对象,为什么?怎么去验证您的看法。
import java.text.*;
import javax.swing.*;public class InnerClassTest
{
public static void main(String[] args)
{
// construct a bank account with initial balance of $10,000
BankAccount account = new BankAccount(10000);
// start accumulating interest at 10%
account.start(10); // keep program running until user selects "Ok"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}class BankAccount
{
/**
Constructs a bank account with an initial balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
} /**
Starts a simulation in which interest is added once per
second
@param rate the interest rate in percent
*/
public void start(double rate)
{
ActionListener adder = new InterestAdder(rate);
Timer t = new Timer(1000, adder);
t.start();
} private double balance; /**
This class adds the interest to the bank account.
The actionPerformed method is called by the timer.
*/
private class InterestAdder implements ActionListener
{
public InterestAdder(double aRate)
{
rate = aRate;
} public void actionPerformed(ActionEvent event)
{
// update interest
double interest = balance * rate / 100;
balance += interest; // print out current balance
NumberFormat formatter
= NumberFormat.getCurrencyInstance();
System.out.println("balance="
+ formatter.format(balance));
} private double rate;
}
}我的问题是:
1,ActionListener adder = new InterestAdder(rate);这条语句中ActionListener是一个接口,InterestAdder是一个实现了这个接口的类,这种定义形式有什么作用,体现了java的什么特性,照我的理解定义为InterestAdder adder = new InterestAdder(rate);也可以呀。
2,InterestAdder是BankAccount的内部类,产生了InterestAdder对象后会不会产生一个BankAccount对象,为什么?怎么去验证您的看法。
解决方案 »
- java 定时任务的问题
- 很简单的一个小程序,关于用户输入并获取数据显示出来
- 【答案贴】看到一个面试题,大家来讨论下!
- 请教高人写一条灵活的sql语句
- 请问java数据库连接jdbc驱动的三个文件放在哪里
- 问JButton如何加载图片
- spring 如何与hibernate 结何呀?
- 关于数据库查询的问题
- 使用JFileChooser选择路径时, 要选择某个路径下的具体文件, 怎样能不选择具体的文件, 只选择到某个文件夹?
- ResultSet中的absolute(int k)不能用,请高人指点.散分.100
- 事件与线程
- 请问大吓们:String 与 StringBuffer 有什么区别??!!
如果还有一个实现了ActionListener接口的类B,要是你不知道到底是InterestAdder类还是B类时,都用ActionListener就可以统一表示了。
2.内部类可以调用上层类的成员,但要求必须是FINAL类型。
可以使代码写的更简洁。可以使用匿名类,这样就可以保证安全性
不会的,运行后只打出了 test
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JFrame{
class inner{
inner(){
System.out.println("inner");
}
}
test(){
System.out.println("test");
}
public static void main(String[] args){
test a=new test();
}
thanks airhand(暴风雨)
thanks wuyafeixue(蓝色天空)