1.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AA extends Applet {
private int count = 0;
private Button
onOff = new Button("Toggle"),
start = new Button("Start");
private TextField t = new TextField(10);
private boolean runFlag = true;
public void init() {
add(t);
start.addActionListener(new StartL());
add(start);
onOff.addActionListener(new OnOffL());
add(onOff);
}
public void go() {
while (true) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e){}
if(runFlag)
t.setText(Integer.toString(count++));
}
}
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
go();
}
}
class OnOffL implements ActionListener {
public void actionPerformed(ActionEvent e) {
runFlag = !runFlag;
}
}
public static void main(String[] args) {
AA applet = new AA();
Frame aFrame = new Frame("AA");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
2。
class SuperClass
{
int a = 3,b = 6;}
class SubClass extends SuperClass
{
// int a=30,b=20;
int max(){return ((a > b)? a:b);}}
public class ABC {
public static void main(String[] args) {
SubClass sb = new SubClass();
System.out.println(sb.max());
}
}
3.
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball( " PingPang " );//interface 里面的变量为public static final
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this .name = name;
}
public void play() {
// ball = new Ball( " Football " );
System.out.println(ball.getName());
}
}
4.
public class BB {
String a ;
public BB(String a){
this.a = a;
System.out.println("11");
}
public static void main(String[] args) {
String s = new String ("Computer");
if(s == "Computer")
System.out.println("Equal A");
if(s.equals("Computer"))
System.out.println("Equal B");
BB b = new BB("aa");
BB c = new BB("aa");
BB d ;
d = c;
if(b == c)
System.out.println("Equal b=c");
if(b.equals("aa"))
System.out.println("Equal b equals 'aa'");
if(d.equals("aa"))
System.out.println("Equal d equals 'aa'");
if(d.equals(c))
System.out.println("Equal d equals 'c'");
if(d==c)
System.out.println("Equal d equals 'c'");
}
}
5.
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
// System.out.println(x);//两个x都匹配,对于父类的变量,可以用super.x来明确,而接口的属性默认隐含为 public static final.所以可以通过A.x来明确。
}
public static void main(String[] args) {
new C().pX();
}
}
6.
public class CalC {
void amethod(){
System.out.println("CalC.amethod");
}
CalC(){//---------〉3
amethod();//----〉4
System.out.println("Hu?");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CalC cc = new CalChild();//--1初始化调用构造方法,向上转型,调用弗雷构造方法
System.out.println("1");
cc.amethod();
}
}
class CalChild extends CalC{
//----2
void amethod(){//-----5
System.out.println("CalChild.amethod");
}
}
7.
class Vehicle{
String str;
public Vehicle(){}
public Vehicle(String s){
}
}
public class Car {
String a;
public Car(String a){this.a = a;}
public static void main(String[] args) {
Vehicle v = new Vehicle("Hello");
Car a;
// a = "a";
v = new Vehicle ("How are You");
v.str = "How is going";
System.out.println("Greeting is :" + v+"11");
}
}
8.
class Parent{
private void method1(){
System.out.println("Parent's method1()");
}
public void method2(){
System.out.println("Parent's method2()");
method1();
}
}
public class Childe extends Parent {
public void method1(){
System.out.println("Child's method1()");
}
public static void main(String[] args) {
Parent p = new Childe();
p.method2();
}
}
9.
class O {
public O() {
System.out.println("A's c is here ");
}
void println() {
System.out.println("A's v is here ");
}
}
class P extends O {
public P() {
System.out.println("B's c is here ");
}
void println() {
System.out.println("B's v is here ");
}
}
public class Chp_4_2 {
public static void main(String[] args) {
O b = new P();
b.println();
}
}
10.
public class Cwich {
/**
* @param args
*/
public static void main(String[] args) {
int x = 9;
switch(x){
default:
System.out.println(1);
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);break;
case 4:
System.out.println("4");
// default:
// break;
}
11.
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest {
public static void main(String[] args) {
Tree tree = new Pine();
if(tree instanceof Pine)
System.out.println("Pine");
if(tree instanceof Tree)
System.out.println("Tree");
if(tree instanceof Oak)
System.out.println("Oak");
else
System.out.println("Oops");
}
}
12.
class FuZhia {
private int a;
public int change(int m) {
return m;
}
}
public class FuZhi extends FuZhia{
public int b;
public static void main() {
FuZhi aa = new FuZhi();
FuZhia bb = new FuZhia();
int k;
k=bb.change(30);
}
}
13.
import java.sql.*;
public class Jdbc
{
String dbUrl="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String theUser="admin";
String thePw="manager";
Connection c=null;
Statement conn;
ResultSet rs=null;
public Jdbc()
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl,theUser,thePw);
conn=c.createStatement();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean executeUpdate(String sql)
{
try
{
conn.executeUpdate(sql);
return true;
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
rs=conn.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public void close()
{
try
{
conn.close();
c.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ResultSet rs;
Jdbc conn = new Jdbc();
rs=conn.executeQuery("select * from test");
try{
while (rs.next())
{
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
14.
class AB {
String s1;
String s2;
AB(String str1,String str2){
s1 = str1;s2 = str2;
}
public String toString(){
return s1+s2;
}
}
public class jiahao {
public static void main(String[] args) {
AB s = new AB("Hello!","I love Java.");
System.out.println(s.toString());
}
}
15.
class N{
N(){
System.out.println("Class N Constructor");
}
}
public class M extends N{
M(){System.out.println("Class M Constructor");}
public static void main(String[] args) {
M m = new M();
}
}
16.
class Base {
private final void f() { //注意final
System.out.println( " Base.f() " );
// public int i;//局部变量前不能放置任何修饰符
}
}
class Derived extends Base {
public final void f() { //注意final
System.out.println( " Derived.f() " );
}
}
public class Main {
public static void main(String[] args) {
Derived op1 = new Derived();
Base op2 = op1;
op1.f();
// op2.f();
}
}
17.
import java.awt.AWTEvent;
import java.awt.TextArea;
import java.awt.event.TextEvent;
public class MyTextArea extends TextArea{
public MyTextArea(int nrows,int ncols){
enableEvents(AWTEvent.TEXT_EVENT_MASK);
}
public void processTextEvent(TextEvent te){
System.out.println("Processing a text event.");
}
}
18.
public class Outer {
final String s = "i am outer class member variable";
public void Method(){
String s1 = "i am inner class variable";
class InnerClass{
String s2 = "i am inner class variable";
public void innerMethod(){
int xyz = 20;
System.out.println(s);
System.out.println("Integer value is " + xyz);
System.out.println(s2);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
19.
public class Outerclass
{
private class InterClass
{
public InterClass()
{
System.out.println("InterClass Create");
}
}
public Outerclass()
{
InterClass ic = new InterClass();
System.out.println("OuterClass Create");
}
public static void main(String[] args)
{
Outerclass oc = new Outerclass();
}
}
20.
public class Parent1 {
public void test(){ }
public Parent1(){
test();
}
public static void main(String[] args){
new Child1();
}
}
class Child1 extends Parent1{
public int instanceValue = 20;
public Child1(){
//super();
System.out.println("instance value isa: " + instanceValue);
}
public void test(){
System.out.println("instance value is: " + instanceValue);
}
}