吃不饱的刘某人 Who say what 算术运算符的使用 位运算符的使用 关系运算符的使用 逻辑运算符的使用 数据类型转换 全局变量和局部变量 比较两个数的大小 给成绩评定等级 计算数学分段函数 使用switch给成绩评定等级 10的阶乘 求和1+1/2-1/3+......+1/100 求和1!+2!+3!+4!+5! 用do-while计算10! 求50~100之间所有素数的程序,目的是演示一下break语句的使用 使用continue语句,计算10以内的奇数的和 计算四位数,前两位一样,后两位一样 判断一个正整数是否是素数,若是计算其阶乘 创建对象与field的访问 简单数据类型作参数,计算立方体体积 圆形类CCircle,计算圆的面积 对象类型作参数 参数的单向值传递 共有成员(method)的建立 私有成员无法从类外部来访问的范例 构造方法的定义与使用 static定义静态变量 通过类名访问类方法 建立获取对象名字的成员方法getName和获取对象性别的成员方法getSex,以及输出对象的全部成员方法studPrint 从一组号码1,2,3……high中,抽出一组幸运号码,将幸运号码作为方法的返回值返回 求一维数组的最大值和最小值 二维数组的赋值与输出 求二维数组的最大值 三维数组 子类可以继承父类的所有非私有数据成员 子类中的同名属性对父类同名属性的隐藏 子类同名方法对父类同名方法的覆盖 子类对父类方法的继承 super的使用 方法的重载 构造方法的继承 构造方法的重载 抽象类的实例 用抽象类类型的变量来建立对象 利用父类的变量数组来访问子类里的内容 接口的实现范例 实现两个以上的接口 接口的扩展 在构造函数里建立内部类的对象 final修饰数据成员 final修饰的最终方法 System的范例:请输入一个字符 object类的范例 数学类 字符串类 判断大小写 Calendar类的应用范例 Random类的范例。随机产生1~6之间的随机整数,统计各数出现的概率 一个简单的框架窗口 通过继承JFrame类实现的窗口(疑惑?) 向框架窗口中添加按钮组件 BorderLayout的布局的应用 GridLayout布局的应用 向FlowLayout布局的容器中添加多个大小不同的组件 通过容器的嵌套实现较复杂的布局 text(you click the button ok)
Who say what
class WhoSayWhat { public static void main ( String args[ ] ) { if ( args. length < 2 ) { System. out. println ( "请向应用程序传递两个参数" ) ; System. exit ( 0 ) ; } String s1 = args[ 0 ] ; String s2 = args[ 1 ] ; System. out. println ( s1 + " Say:" + s2) ; }
}
算术运算符的使用
public class ArithOp { public static void main ( String[ ] args) { int a = 7 + 2 ; int b = a* 2 ; int c = b / 9 ; int d = - a; int e = d % 2 ; double f = 17.5 / 4 ; int i = 2 ; int j = i ++ ; int k = ++ i; System. out. println ( "a=" + a) ; System. out. println ( "b=" + b) ; System. out. println ( "c=" + c) ; System. out. println ( "d=" + d) ; System. out. println ( "e=" + e) ; System. out. println ( "f=" + f) ; System. out. println ( "i=" + i) ; System. out. println ( "j=" + j) ; System. out. println ( "k=" + k) ; }
}
位运算符的使用
public class BitOp { public static void main ( String[ ] args) { int a = 9 ; int b = 15 ; int c = 8 ; int d, e, f, g, h; d = a & b; e = a | b; f = a ^ b; g = a << 2 ; h = c >> 1 ; System. out. println ( "d=" + d) ; System. out. println ( "e=" + e) ; System. out. println ( "f=" + f) ; System. out. println ( "g=" + g) ; System. out. println ( "h=" + h) ; }
}
关系运算符的使用
public class RelationOp { public static void main ( String[ ] args) {
int a = 9 ; int b = 6 ; int c = 6 ; boolean d = a > b; boolean e = a < b; boolean f = b == c; boolean g = b != c; boolean h = b >= c; boolean i = b <= c; boolean j = a == b; System. out. println ( "d=" + d) ; System. out. println ( "e=" + e) ; System. out. println ( "f=" + f) ; System. out. println ( "g=" + g) ; System. out. println ( "h=" + h) ; System. out. println ( "i=" + i) ; System. out. println ( "j=" + j) ; }
}
逻辑运算符的使用
public class LogicOp { public static void main ( String[ ] args) { int a = 9 ; int b = 6 ; int c = 6 ; boolean d, e, f, g; d = ! ( a > b) ; e = ( a > b) && ( a > c) ; f = ( b == c) || ( a < b) ; g = ( b == c) && ( a < b) ; System. out. println ( "d=" + d) ; System. out. println ( "e=" + e) ; System. out. println ( "f=" + f) ; System. out. println ( "g=" + g) ; }
}
数据类型转换
public class TypeConversion { public static void main ( String[ ] args) { char a = 1 ; byte b = 2 ; short c = 3 ; int d = 4 ; byte e; e = ( byte ) ( a + b + c + d) ; short f; f = ( short ) ( a + b + c + d) ; int g; g = a + b + c + d; float h; h = a + b + c + d; double i; i = a + b + c + d; System. out. println ( "e = " + e) ; System. out. println ( "f = " + f) ; System. out. println ( "g = " + g) ; System. out. println ( "h = " + h) ; System. out. println ( "i = " + i) ; }
}
全局变量和局部变量
public class SetVariable
{
static double pi = 3.141592654 ;
static short s1; static int i1; static long l1; static char ch1; static float f1; static double d1; static boolean b1; public static void main ( String args[ ] ) { short s2 = 35 ; int i2 = - 32 ; long l2 = 34555 L; char ch2 = 'A' ; float f2 = 897.89F ; double d2 = 34.345 ; boolean b2 = false ; System. out. println ( "数学常量pi = " + pi) ; System. out. println ( "******局部变量******" ) ; System. out. println ( "短整型变量s2 = " + s2) ; System. out. println ( "整型变量i2 = " + i2) ; System. out. println ( "长整型变量l2 = " + l2) ; System. out. println ( "字符变量ch2 = " + ch2) ; System. out. println ( "浮点数类型f2 = " + f2) ; System. out. println ( "双精度型变量d2 = " + d2) ; System. out. println ( "布尔型变量b2 = " + b2) ; change ( ) ; System. out. println ( "******全局变量******" ) ; System. out. println ( "短整型变量s1 = " + s1) ; System. out. println ( "整型变量i1 = " + i1) ; System. out. println ( "长整型变量l1 = " + l1) ; System. out. println ( "字符变量ch1 = " + ch1) ; System. out. println ( "浮点数类型f1 = " + f1) ; System. out. println ( "双精度型变量d1 = " + d1) ; System. out. println ( "布尔型变量b1 = " + b1) ; } public static void change ( ) { s1 = 125 ; i1 = 88 ; l1 = 987654321 L; ch1 = 'B' ; f1 = 3.2590F ; d1 = - 1.04E-5 ; b1 = true ; }
}
比较两个数的大小
public class Ifdemo1 { public static void main ( String[ ] args) { int i1 = 8 , i2 = 5 ; if ( i1 >= i2) System. out. println ( i1 + ">=" + i2) ; else System. out. println ( i1 + "<" + i2) ; }
}
给成绩评定等级
public class IfDemo2 {
public static void main ( String[ ] args) { int testscore = 78 ; char grade; if ( testscore >= 90 ) grade = 'A' ; else if ( testscore >= 80 ) grade = 'B' ; else if ( testscore >= 70 ) grade = 'C' ; else if ( testscore >= 60 ) grade = 'D' ; else grade = 'E' ; System. out. println ( "Grade = " + grade) ; }
}
计算数学分段函数
public class Ifdemo3 { public static void main ( String[ ] args) { int x = 4 ; float y; if ( x <= 0 ) y = x + 8 ; else if ( x <= 6 ) y = 3 * x- 21 ; else y = 8 * x * x - 9 ; System. out. println ( "y = " + y) ; }
}
使用switch给成绩评定等级
public class SwitchDemo { public static void main ( String[ ] args) { int testscore = 78 ; char grade; switch ( testscore / 10 ) { case 10 : case 9 : grade = ' A' ; break ; case 7 : grade = 'C' ; break ; case 6 : grade = 'D' ; break ; default : grade = 'E' ; } System. out. println ( "Grade = " + grade) ; }
}
10的阶乘
public class ForOp1 { public static void main ( String[ ] args) { long result = 1 ; for ( int i = 10 ; i > 0 ; i-- ) result *= i ; System. out. println ( "10!=" + result) ; }
}
求和1+1/2-1/3+…+1/100
public class ch2 {
public static void main ( String[ ] args) {
int m = 1 ; double s = 0 ; for ( int i = 1 ; i <= 100 ; i ++ ) { s = s + m * 1.0 / i; m = - m; } System. out. println ( "s =" + s) ; }
}
求和1!+2!+3!+4!+5!
public class ch3n { public static void main ( String[ ] args) { int i, t, s= 0 ; for ( i = 1 , t = 1 ; i <= 5 ; i ++ ) { t = t * i; s + = t; } System. out. println ( " s = " + s) ; }
}
用do-while计算10!
public class DoWhileDemo { public static void main ( String[ ] args) { int n = 10 ; long result = 1 ; do result * = n-- ; while ( n >= 1 ) ; System. out. println ( "10!=" + result) ; }
}
public class WhileDemo { public static void main ( String[ ] args) { int n = 10 ; long result = 1 ; while ( n >= 1 ) result * = n-- ; System. out. println ( "10! = " + result) ; }
}
求50~100之间所有素数的程序,目的是演示一下break语句的使用
public class Prime50_100Exam { public static void main ( String[ ] args) { int n, m, i; for ( n= 50 ; n< 100 ; n++ ) { for ( i= 2 ; i<= n/ 2 ; i++ ) { if ( n% i== 0 ) break ; } if ( i> n/ 2 ) { System. out. print ( n+ " " ) ; } } }
}
使用continue语句,计算10以内的奇数的和
public class ContinueOp { public static void main ( String[ ] args) { int s = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { if ( i % 2 == 0 ) continue ; s += i ; } System. out. println ( "s = " + s) ; }
}
计算四位数,前两位一样,后两位一样
public class first { public static void main ( String[ ] args) { int a, i, x, y, z, t; for ( i= 32 ; i< 100 ; i++ ) { a= i* i; x= a/ 1000 ; y= a/ 100 % 10 ; z= a/ 10 % 10 ; t= a% 10 ; if ( x== y&& z== t) { System. out. print ( i+ "的平方是" + a) ; } } }
}
判断一个正整数是否是素数,若是计算其阶乘
public class Math_mothodExam { public static boolean prime ( int n) { for ( int i= 2 ; i< n/ 2 ; i++ ) { if ( n% i== 0 ) return false ; } return true ; } public static int factorial ( int n) { if ( n<= 1 ) return 1 ; int m= 1 ; for ( int i= 1 ; i<= n; i++ ) m*= i; return m; } public static void main ( String args[ ] ) { int n= 13 ; System. out. println ( n+ "是素数吗?" + prime ( n) ) ; if ( prime ( n) ) System. out. println ( n+ "!=" + factorial ( n) ) ; }
}
创建对象与field的访问
class CRectangle { int width; int height;
}
public class app7_1 { public static void main ( String args[ ] ) { CRectangle rect1; rect1= new CRectangle ( ) ; rect1. width= 10 ; rect1. height= 5 ; System. out. println ( "width=" + rect1. width) ; System. out. println ( "height=" + rect1. height) ; }
}
简单数据类型作参数,计算立方体体积
class cube { double width; double height; double depth; double volume ( ) { return width * height * depth; } void setParam ( double x, double y, double z) { width = x; height = y; depth = z; } }
public class Usecube { public static void main ( String args[ ] ) { double v; cube mycube = new cube ( ) ; mycube. setParam ( 4 , 6 , 8 ) ; v = mycube. volume ( ) ; System. out. println ( "mycube.volume = " + v) ; } }
圆形类CCircle,计算圆的面积
class CCircle
{ double pi; double radius; double getRadius ( ) { return radius; } void setCircle ( double r, double p) { pi= p; radius= r; }
} public class Ccircle_main
{ public static void main ( String args[ ] ) { CCircle cir1= new CCircle ( ) ; cir1. setCircle ( 2.0 , 3.1416 ) ; System. out. println ( "radius=" + cir1. getRadius ( ) ) ; }
}
对象类型作参数
class Point { int x, y;
}
class Carry { void fun ( Point p) { p. x += p. y; p. y *= 5 ; }
}
public class UsePoint { public static void main ( String args[ ] ) { carry s = new carry ( ) ; point p = new Point ( ) ; p. x = 3 ; p. y = 5 ; s. fun ( p) ; System. out. println ( "p.x = " + p. x + ", p.y = " + p. y) ; }
}
参数的单向值传递
public class Carry { static void fun ( int x, int y) { x = x + y; y = 5 * y; System. out. println ( "x = " + x + ", y = " + y) ; } public static void main ( String args[ ] ) { int a = 3 ; int b = 5 ; fun ( a, b) ; System. out. println ( "a = " + a+ ", b = " + b) ; }
}
共有成员(method)的建立
class CCircle
{ private double pi= 3.14 ; private double radius; public void show_area ( ) { System. out. println ( "area=" + pi* radius* radius) ; } public void setRadius ( double r) { if ( r> 0 ) { radius= r; System. out. println ( "radius=" + radius) ; } else System. out. println ( "input error" ) ; }
} public class Gongyou
{ public static void main ( String args[ ] ) { CCircle cir1= new CCircle ( ) ; cir1. setRadius ( - 2.0 ) ; cir1. show_area ( ) ; }
}
私有成员无法从类外部来访问的范例
class CCircle
{ private double pi= 3.14 ; private double radius; void show_area ( ) { System. out. println ( "area=" + pi* radius* radius) ; }
}
public class Siyou
{ public static void main ( String args[ ] ) { CCircle cir1= new CCircle ( ) ; cir1. radius= - 2.0 ; cir1. show_area ( ) ; }
}
构造方法的定义与使用
class Dog { private int weight; public Dog ( ) { weight = 42 ; } public int getWeight ( ) { return weight; } }
public class UseDog { public static void main ( String args[ ] ) { Dog d = new Dog ( ) ; System. out. println ( "The dog’s weight is " + d. getWeight ( ) ) ; } }
static定义静态变量
class SDemo { int x; static int y;
}
public class StaticDemo { public static void main ( String arg[ ] ) { SDemo ob1 = new SDemo ( ) ; SDemo ob2 = new SDemo ( ) ; ob1. x = 10 ; ob2. x = 20 ; System. out. println ( "Of course ,ob1.x and ob2.x " + ob1. x+ ob2. x) ; System. out. println ( "The static var is shared" ) ; ob1. y = 19 ; System. out. println ( "ob1.y=" + ob1. y) ; System. out. println ( "ob2.y=" + ob2. y) ; SDemo. y = 11 ; System. out. println ( "SDemo.y = " + SDemo. y) ; System. out. println ( "ob1.y = " + ob1. y) ; System. out. println ( "ob2.y = " + ob2. y) ; }
}
通过类名访问类方法
class SMeth { static int val= 1024 ; static int valDiv2 ( ) { return val / 2 ; } }
public class Staicmeth { public static void main ( String args[ ] ) { System. out. println ( "Val is" + SMeth. val) ; System. out. println ( "StaticMeth.valDiv2():" + SMeth. valDiv2 ( ) ) ; }
}
建立获取对象名字的成员方法getName和获取对象性别的成员方法getSex,以及输出对象的全部成员方法studPrint
class Student { private String name; private char sex; private double score; Student ( String cname, char csex, double cscore) { name = cname; sex = csex; score = cscore; } String getName ( ) { return name; } char getSex ( ) { return sex; } void studPrint ( ) { System. out. println ( "Name:" + name + "\tSex:" + sex + "\tScore:" + score) ; }
}
public class rrOb { public static void main ( String args[ ] ) { String mname; char msex; int len; Student[ ] st1 = new Student [ 3 ] ; st1[ 0 ] = new Student ( "Li" , 'F' , 89 ) ; st1[ 1 ] = new Student ( "Zhang" , 'M' , 90 ) ; st1[ 2 ] = new Student ( "Zhou" , 'F' , 98 ) ; for ( int i = 0 ; i < st1. length; i ++ ) st1[ i] . studPrint ( ) ; mname = st1[ 1 ] . getName ( ) ; msex = st1[ 1 ] . getSex ( ) ; System. out. println ( "Name 1:" + mname + "\t" + msex) ; }
}
从一组号码1,2,3……high中,抽出一组幸运号码,将幸运号码作为方法的返回值返回
import java. util. Arrays;
public class RandomA { public static int [ ] drawing ( int high, int number) { int I; int numbers[ ] = new int [ high] ; int result[ ] = new int [ number] ; for ( i = 0 ; i < high; i ++ ) numbers[ I] = i + 1 ; for ( i = 0 ; i < number; i ++ ) { int j = ( int ) ( Math. random ( ) * ( high- i) ) ; result[ i] = numbers[ j] ; numbers[ j] = numbers[ high- 1 - i] ; } return result; } public static void main ( String args[ ] ) { int numbers = 7 ; int topNumber = 36 ; int a[ ] = drawing ( topNumber, numbers) ; Arrays. sort ( a) ; System. out. print ( "Lucky numbers are: " ) ; for ( int i = 0 ; i < a. length; i ++ ) System. out. print ( " " + a[ i] ) ; System. out. println ( ) ; }
}
求一维数组的最大值和最小值
public class Arraymax { public static void main ( String args[ ] ) { int table[ ] = { 21 , 34 , 65 , 38 , 24 , 46 , 29 , 64 , 75 , 92 } ; int i, max, min; System. out. print ( "Table: " ) ; for ( i = 0 ; i < table. length; i ++ ) System. out. print ( " " + table[ i] ) ; System. out. println ( ) ; max = min = table[ 0 ] ; for ( i = 1 ; i < table. length; i ++ ) { if ( table[ i] > max) max = table[ i] ; else if ( table[ i] < min) min = table[ i] ; } System. out. println ( "Max=" + max) ; System. out. println ( "Min=" + min) ; }
}
二维数组的赋值与输出
public class ArrIO { public static void main ( String args[ ] ) { int a[ ] [ ] = new int [ 4 ] [ ] ; for ( int I = 0 ; I < a. length; I ++ ) { a[ I] = new int [ 5 ] ; for ( int j = 0 ; j < a[ I] . length; j ++ ) a[ I] [ j] = I+ j; } for ( int k = 0 ; k < a. length; k ++ ) { for ( int n = 0 ; n< a[ k] . length; n ++ ) System. out. print ( "a[" + k+ "][" + n+ "]=" + a[ k] [ n] + " " ) ; System. out. println ( ) ; } }
}
求二维数组的最大值
class Maxvalue { int maxv ( int arr1[ ] [ ] ) { int i, j, max; max = arr1[ 0 ] [ 0 ] ; for ( i = 0 ; i < arr1. length; i ++ ) for ( j = 0 ; j < arr1[ i] . length; j ++ ) if ( arr1[ i] [ j] > max) max = arr1[ i] [ j] ; return max; }
}
public class ArrMax { public static void main ( String args[ ] ) { int a[ ] [ ] = { { 1 , 3 , 5 } , { 2 , 5 , 7 , 8 } , { 8 , 3 , 4 , 6 , 9 } } ; int max; Maxvalue p = new Maxvalue ( ) ; max = p. maxv ( a) ; System. out. println ( "max=" + max) ; }
}
三维数组
public class Sanwei { public static void main ( String args[ ] ) { int i, j, k, sum= 0 ; int A[ ] [ ] [ ] = { { { 5 , 1 } , { 6 , 7 } } , { { 9 , 4 } , { 8 , 3 } } } ; for ( i= 0 ; i< A. length; i++ ) for ( j= 0 ; j< A[ i] . length; j++ ) for ( k= 0 ; k< A[ j] . length; k++ ) { System. out. print ( "A[" + i+ "][" + j+ "][" + k+ "]=" ) ; System. out. println ( A[ i] [ j] [ k] ) ; sum+= A[ i] [ j] [ k] ; } System. out. println ( "sum=" + sum) ; }
}
子类可以继承父类的所有非私有数据成员
class A1 { int x = 25 ; private int z;
}
public class SubDemo1 extends A1 { public static void main ( String args[ ] ) { subDemo1 p = new subDemo1 ( ) ; System. out. println ( "p.x = " + p. x) ; System. out. println ( “p. z = ” + p. z) ; }
}
子类中的同名属性对父类同名属性的隐藏
class A { int x1 = 10 ; float x2 = 2.0f ;
}
class B extends A { int x1 = 15 ; int x2 = 25 ; String x3 = "hello" ;
}
public class C { public static void main ( String args[ ] ) { A p1 = new A ( ) ; B p2 = new B ( ) ; System. out. println ( "A:" + p1. x1 + p1. x2) ; System. out. println ( "B:" + p2. x1 + p2. x2 + p2. x3) ; }
}
子类同名方法对父类同名方法的覆盖
class A { int fun ( int a, int b) { return a * b; }
}
class B extends A { int fun ( int c, int d) { return c + d; }
}
public class C2 { public static void main ( String args[ ] ) { B s = new B ( ) ; System. out. println ( "s = " + s. fun ( 10 , 20 ) ) ; }
}
子类对父类方法的继承
class A2 { int x = 0 , y = 1 ; void print1 ( ) { System. out. println ( "x = " + x + "y = " + y) ; } private void print2 ( ) { System. out. println ( "x = " + x + "y = " + y) ; } }
public class SubDemo2 extends A2 { public static void main ( String args[ ] ) { int z = 3 ; SubDemo2 p = new SubDemo2 ( ) ; p. print1 ( ) ; p. print2 ( ) ; } }
super的使用
class A { int x1 = 10 ; float x2 = 2.0f ; void show ( ) { System. out. println ( "1 A:" + "x1 = " + x1 + "x2 = " + x2) ;
} }
class B extends A { int x1 = 15 ; int x2 = 25 ; void show ( ) { System. out. println ( "2 A:" + "x1 = " + super . x1 + "x2 = " + super . x2) ; super . show ( ) ; System. out. println ( "3 B:" + "x1 = " + x1 + "x2 = " + x2) ;
} }
public class SuperDemo { public static void main ( String args[ ] ) { A p = new A ( ) ; B q = new B ( ) ; p. show ( ) ; q. show ( ) ; }
}
方法的重载
class A { int add ( int a, int b) { return a + b; } double add ( double a, double b) { return a + b; } double add ( double a, double b, double c) { return a + b + c; }
}
public class OverloadDemo { public static void main ( String args[ ] ) { A p = new A ( ) ; System. out. println ( "Sum is " + p. add ( 3.8 , 5.3 ) ) ; System. out. println ( "Sum is " + p. add ( 3 , 5 ) ) ; System. out. println ( "Sum is " + p. add ( 3.8 , 5.3 , 7.2 ) ) ; }
}
构造方法的继承
class AddClass { public int x = 0 , y = 0 , z = 0 ; AddClass ( int x) { this . x = x; } AddClass ( int x, int y) { this . x = x; this . y = y; } AddClass ( int x, int y, int z) { this . x = x; this . y = y; this . z = z; } public int add ( ) { return x + y + z; }
}
public class Subconstruct extends AddClass { int a = 0 , b = 0 , c = 0 ; Subconstruct ( int x) { super ( x) ; a = x + 7 ; } Subconstruct ( int x, int y) { super ( x, y) ; a = x + 5 ; b = y + 5 ; } Subconstruct ( int x, int y, int z) { super ( x, y, z) ; a = x + 4 ; b = y + 4 ; c = z + 4 ; } public int add ( ) { System. out. println ( "super: x + y + z = " + super . add ( ) ) ; return a + b + c; } public static void main ( String args[ ] ) { Subconstruct p1 = new Subconstruct ( 2 , 3 , 5 ) ; Subconstruct p2 = new Subconstruct ( 10 , 20 ) ; Subconstruct p3 = new Subconstruct ( 1 ) ; System. out. println ( "a + b + c = " + p1. add ( ) ) ; System. out. println ( "a + b= " + p2. add ( ) ) ; System. out. println ( "a = " + p3. add ( ) ) ; }
}
构造方法的重载
class AddClass { public int x = 0 , y = 0 , z = 0 ; AddClass ( int x) { this . x = x; } AddClass ( int x, int y) { this ( x) ; this . y = y; } AddClass ( int x, int y, int z) { this ( x, y) ; this . z = z; } public int add ( ) { return x+ y+ z; }
}
public class ConstructOver { public static void main ( String args[ ] ) { AddClass p1 = new AddClass ( 2 , 3 , 5 ) ; AddClass p2 = new AddClass ( 10 , 20 ) ; AddClass p3 = new AddClass ( 1 ) ; System. out. println ( " x + y + z = " + p1. add ( ) ) ; System. out. println ( " x + y = " + p2. add ( ) ) ; System. out. println ( " x = " + p3. add ( ) ) ; }
}
抽象类的实例
abstract class CShape
{ protected String color; public void setColor ( String str) { color= str; } abstract void show ( ) ;
} class CRectangle extends CShape
{ int width, height; public CRectangle ( int w, int h) { width= w; height= h; } public void show ( ) { System. out. print ( "color=" + color+ ", " ) ; System. out. println ( "area=" + width* height) ; }
} class CCircle extends CShape
{ double radius; public CCircle ( double r) { radius= r; } public void show ( ) { System. out. print ( "color=" + color+ ", " ) ; System. out. println ( "area=" + 3.14 * radius* radius) ; }
} public class app1 { public static void main ( String args[ ] ) { CRectangle rect= new CRectangle ( 5 , 10 ) ; rect. setColor ( "Yellow" ) ; rect. show ( ) ; CCircle cir= new CCircle ( 2.0 ) ; cir. setColor ( "Green" ) ; cir. show ( ) ; }
}
用抽象类类型的变量来建立对象
public class app2 { public static void main ( String args[ ] ) { CShape shape1= new CRectangle ( 5 , 10 ) ; shape1. setColor ( "Yellow" ) ; shape1. show ( ) ; CShape shape2= new CCircle ( 2.0 ) ; shape2. setColor ( "Green" ) ; shape2. show ( ) ; }
}
利用父类的变量数组来访问子类里的内容
public class app3
{ public static void main ( String args[ ] ) { CShape shape[ ] ; shape= new CShape [ 2 ] ; shape[ 0 ] = new CRectangle ( 5 , 10 ) ; shape[ 0 ] . setColor ( "Yellow" ) ; shape[ 0 ] . show ( ) ; shape[ 1 ] = new CCircle ( 2.0 ) ; shape[ 1 ] . setColor ( "Green" ) ; shape[ 1 ] . show ( ) ; }
}
接口的实现范例
interface iShape2D { final double pi= 3.14 ; abstract void area ( ) ;
} class CRectangle implements iShape2D { int width, height; public CRectangle ( int w, int h) { width= w; height= h; } public void area ( ) { System. out. println ( "area=" + width* height) ; }
} class CCircle implements iShape2D { double radius; public CCircle ( double r) { radius= r; } public void area ( ) { System. out. println ( "area=" + pi* radius* radius) ; }
} public class app4 { public static void main ( String args[ ] ) { CRectangle rect= new CRectangle ( 5 , 10 ) ; rect. area ( ) ; CCircle cir= new CCircle ( 2.0 ) ; cir. area ( ) ; }
}
实现两个以上的接口
interface iShape2D
{ final double pi= 3.14 ; abstract void area ( ) ;
} interface iColor
{ abstract void setColor ( String str) ;
} class CCircle implements iShape2D , iColor
{ double radius; String color; public CCircle ( double r) { radius= r; } public void setColor ( String str) { color= str; System. out. println ( "color=" + color) ; } public void area ( ) { System. out. println ( "area=" + pi* radius* radius) ; }
} public class app5
{ public static void main ( String args[ ] ) { CCircle cir; cir= new CCircle ( 2.0 ) ; cir. setColor ( "Blue" ) ; cir. area ( ) ; }
}
接口的扩展
interface iShape
{ final double pi= 3.14 ; abstract void setColor ( String str) ;
} interface iShape2D extends iShape
{ abstract void area ( ) ;
} class CCircle implements iShape2D
{ double radius; String color; public CCircle ( double r) { radius= r; } public void setColor ( String str) { color= str; System. out. println ( "color=" + color) ; } public void area ( ) { System. out. println ( "area=" + pi* radius* radius) ; }
} public class app6
{ public static void main ( String args[ ] ) { CCircle cir; cir= new CCircle ( 2.0 ) ; cir. setColor ( "Blue" ) ; cir. area ( ) ; }
}
在构造函数里建立内部类的对象
public class inner { public inner ( ) { Caaa aa= new Caaa ( ) ; aa. set_num ( 5 ) ; } public static void main ( String args[ ] ) { inner obj= new inner ( ) ; } class Caaa { int num; void set_num ( int n) { num= n; System. out. println ( "num= " + num) ; } }
}
final修饰数据成员
class ca { static int n = 20 ; final int nn; final int k = 40 ; ca ( ) { nn = + + n; }
}
public class finalDemo1 { public static void main ( String args[ ] ) { ca m1 = new ca ( ) ; ca m2 = new ca ( ) ; m1. nn = 90 ; System. out. println ( "m2.nn = " + m2. nn) ; System. out. println ( "m2.k = " + m2. k) ; System. out. println ( "m1.nn = " + m1. nn) ; System. out. println ( "m1.k = " + m1. k) ; }
}
final修饰的最终方法
class a1 { final int add ( int x, int y) { return x + y; } int mul ( int a, int b) { int z = 0 ; z = add ( 1 , 7 ) + a * b; return z; }
}
public class finalDemo2 extends a1 { int add ( int x, int y) { return x + y + 2 ; } public static void main ( String args[ ] ) { int a = 2 , b = 3 , z1, z2; finalDemo2 p1 = new finalDemo2 ( ) ; z1 = p1. add ( a, b) ; z2 = p1. mul ( a, b) ; System. out. println ( "z1 = " + z1) ; System. out. println ( "z2 = " + z2) ; }
}
System的范例:请输入一个字符
public class SystemD { public static void main ( String args[ ] ) throws Exception { char c; System. out. println ( "请输入一个字符:" ) ; c = ( char ) System. in. read ( ) ; System. out. println ( "你输入字符:" + c) ; }
}
object类的范例
public class ObjectD { public static void main ( String[ ] args) { Integer a = new Integer ( 1 ) ; Integer b = new Integer ( 1 ) ; System. out. println ( a. equals ( b) ) ; System. out. println ( "The Object’s class is: " + a. getClass ( ) ) ; }
}
数学类
class MathD { public static void main ( String args[ ] ) { System. out. println ( "Math.E=" + Math. E) ; System. out. println ( "Math.PI=" + Math. PI) ; System. out. println ( "sin(pi/2) =" + Math. sin ( Math. PI/ 2 ) ) ; System. out. println ( "ceil(E)=" + Math. ceil ( Math. E) ) ; System. out. println ( "rint(PI)=" + Math. rint ( Math. PI) ) ; System. out. println ( "round(PI)=" + Math. round ( Math. PI) ) ; int I = ( int ) ( Math. random ( ) * 10 ) + 1 ; System. out. println ( "i =" + i ) ; System. out. println ( "exp(1)=" + Math. exp ( 1 ) ) ; System. out. println ( "lnE=" + Math. log ( Math. E) ) ; System. out. println ( "sqrt(" + 25 + ")=" + Math. sqrt ( 25 ) ) ; System. out. println ( "power(" + 2 + "," + 8 + ")=" + Math. pow ( 2 , 8 ) ) ; System. out. println ( "abs(-8.2)=" + Math. abs ( - 8.2 ) ) ; System. out. println ( "max(" + 2 + "." + "8)=" + Math. max ( 2 , 8 ) ) ; System. out. println ( "min(" + 2 + "." + "8)=" + Math. min ( 2 , 8 ) ) ; }
}
字符串类
public class StringD { public static void main ( String args[ ] ) { String s1 = "Java " ; String s2 = "java" ; String s3 = "Welcome" ; String s4 = "Welcome" ; String s5 = "Welcoge" ; String sc1 = s3. concat ( s1) ; String sc2 = s1. concat ( "abx" ) ; String str1 = s3. replace ( 'e' , 'r' ) ; String w1 = s5. toLowerCase ( ) ; String u2 = s2. toUpperCase ( ) ; System. out. println ( "s1=" + s1 + "\t s2=" + s2) ; System. out. println ( "s3=" + s3+ "\t s4=" + s4) ; System. out. println ( "s5=" + s5) ; System. out. println ( "s3+s1=" + sc1) ; System. out. println ( "s1+abx=" + sc2) ; System. out. println ( "s3.replace('e', 'r')= " + str1) ; System. out. println ( "s5.toLower=" + w1) ; System. out. println ( "s2.toUpper=" + u2) ; }
}
判断大小写
public class CharacterD { public static void main ( String args[ ] ) { Character ch = new Character ( 'a' ) ; char c = ch. charValue ( ) ; if ( Character. isUpperCase ( c) ) System. out. println ( "The character " + c + " is upper case." ) ; else System. out. println ( "The character" + c + "is lower case." ) ; boolean b = Character. isDigit ( c) ; int x = Character. digit ( 'c' , 16 ) ; System. out. println ( "b=" + b) ; System. out. println ( "x=" + x) ; }
}
Calendar类的应用范例
import java. util. *; class CalendarD { public static void main ( String args[ ] ) { Calendar now = Calendar. getInstance ( ) ; int ampm = now. get ( Calendar. AM_PM) ; int hour = now. get ( Calendar. HOUR_OF_DAY) ; int day = now. get ( Calendar. DAY_OF_MONTH) ; System. out. println ( ampm) ; System. out. println ( hour) ; System. out. println ( day) ; }
}
Random类的范例。随机产生1~6之间的随机整数,统计各数出现的概率
import java. util. *; class RandomDemo { public static void main ( String args[ ] ) { Random rd = new Random ( ) ; int [ ] fre = new int [ 6 ] ; int rdGet; for ( int i = 0 ; i <= 100 ; i ++ ) { rdGet = Math. abs ( rd. nextInt ( ) ) % 6 + 1 ; switch ( rdGet) { case 1 : fre[ 0 ] ++ ; break ; case 2 : fre[ 1 ] ++ ; break ; case 3 : fre[ 2 ] ++ ; break ; case 4 : fre[ 3 ] ++ ; break ; case 5 : fre[ 4 ] ++ ; break ; case 6 : fre[ 5 ] ++ ; break ; } } for ( int j = 0 ; j < fre. length; j ++ ) { System. out. println ( ( j+ 1 ) + "出现的次数" + fre[ j] + "出现的比率:" + fre[ j] / 10.0 + "% " ) ; }
}
一个简单的框架窗口
import javax. swing. *; public class SimpleFrame { public static void main ( String args[ ] ) { JFrame frame= new JFrame ( "Simple Frame " ) ; frame. setSize ( 350 , 240 ) ; frame. setVisible ( true ) ; }
}
通过继承JFrame类实现的窗口(疑惑?)
import javax. swing. *;
public class SimpleFrame extends JFrame { public SimpleFrame ( ) { this ( "No Title" ) ; } public SimpleFrame ( String title) { super ( title) ; setSize ( 350 , 240 ) ; setLocation ( 350 , 250 ) ; setVisible ( true ) ; setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; } }
public class SimpleFrameTest { public static void main ( String args[ ] ) { SimpleFrame frame= new SimpleFrame ( "Simple Frame" ) ; } }
向框架窗口中添加按钮组件
import java. awt. Container;
import javax. swing. *;
public class FrameWithButton extends JFrame { JButton jButton1, jButton2; Container cp= null; public FrameWithButton ( ) { this ( “No Title”) ; } public FrameWithButton ( String title) { super ( title) ; jButton1 = new JButton ( “OK”) ; jButton2 = new JButton ( “Cancel”) ; cp = getContentPane ( ) ; cp. add ( jButton1) ; cp. add ( jButton2) ; setSize ( 400 , 300 ) ; setVisible ( true ) ; setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; } public static void main ( String[ ] args) { JFrame frame = new FrameWithButton ( "Frame With Button" ) ; }
}
BorderLayout的布局的应用
import java. awt. *;
import javax. swing. *; public class BorderDemo extends JFrame { JButton jButton1, jButton2, jButton3, jButton4, jButton5; public BorderDemo ( ) { Container cp = getContentPane ( ) ; jButton1 = new JButton ( "North" ) ; jButton2 = new JButton ( "South" ) ; jButton3 = new JButton ( "West" ) ; jButton4 = new JButton ( "East" ) ; jButton5 = new JButton ( "Center" ) ; cp. add ( jButton1, BorderLayout. NORTH) ; cp. add ( jButton2, BorderLayout. SOUTH) ; cp. add ( jButton3, BorderLayout. WEST) ; cp. add ( jButton4, BorderLayout. EAST) ; cp. add ( jButton5, BorderLayout. CENTER) ; setSize ( 400 , 300 ) ; setTitle ( "BorderLayout Demo" ) ; setVisible ( true ) ; setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; } public static void main ( String args[ ] ) { BorderDemo window = new BorderDemo ( ) ; }
}
GridLayout布局的应用
import java. awt. *;
import javax. swing. *; public class GridWindow extends JFrame { public GridWindow ( ) { Container cp = getContentPane ( ) ; cp. setLayout ( new GridLayout ( 0 , 2 ) ) ; cp. add ( new JButton ( "Button 1" ) ) ; cp. add ( new JButton ( "2" ) ) ; cp. add ( new JButton ( "Button 3" ) ) ; cp. add ( new JButton ( "Long-Named Button 4" ) ) ; cp. add ( new JButton ( "Button 5" ) ) ; } public static void main ( String args[ ] ) { GridWindow window = new GridWindow ( ) ; window. setTitle ( "GridLayout" ) ; window. pack ( ) ; window. setVisible ( true ) ; }
}
向FlowLayout布局的容器中添加多个大小不同的组件
import java. awt. *;
import javax. swing. *; public class FlowWindow extends JFrame { public FlowWindow ( ) { Container cp = getContentPane ( ) ; FlowLayout layout = new FlowLayout ( FlowLayout. CENTER, 10 , 20 ) ; cp. setLayout ( layout) ; cp. add ( new JButton ( "Button 1" ) ) ; cp. add ( new JButton ( "2" ) ) ; cp. add ( new JButton ( "Button 3" ) ) ; cp. add ( new JButton ( "Long-Named Button 4" ) ) ; cp. add ( new JButton ( "Button 5" ) ) ; setSize ( 400 , 300 ) ; setVisible ( true ) ; setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; } public static void main ( String args[ ] ) { FlowWindow window = new FlowWindow ( ) ; }
}
通过容器的嵌套实现较复杂的布局
import java. awt. *;
import javax. swing. *; public class FrameWithPanel extends JFrame { JButton btn1, btn2, btn3; Container cp = null; JPanel panel_1, panel_2; public FrameWithPanel ( ) { super ( "Frame With Panel" ) ; btn1 = new JButton ( "Red" ) ; btn2 = new JButton ( "Green" ) ; btn3 = new JButton ( "Blue" ) ; panel_1 = new JPanel ( ) ; panel_1. setBackground ( Color. CYAN) ; panel_2 = new JPanel ( ) ; panel_2. setLayout ( new FlowLayout ( FlowLayout. CENTER, 20 , 10 ) ) ; cp= getContentPane ( ) ; panel_2. add ( btn1) ; panel_2. add ( btn2) ; panel_2. add ( btn3) ; cp. add ( panel_1, BorderLayout. CENTER) ; cp. add ( panel_2, BorderLayout. SOUTH) ; setSize ( 300 , 200 ) ; } public static void main ( String args[ ] ) { FrameWithPanel frame = new FrameWithPanel ( ) ; frame. setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; frame. setVisible ( true ) ; }
}
text(you click the button ok)
import java. awt. *;
import java. awt. event. *;
import javax. swing. *; public class EventDemo extends JFrame implements ActionListener { JTextField tf ; JButton btn1, btn2; public EventDemo ( ) { super ( "Event Demo" ) ; tf = new JTextField ( 20 ) ; btn1 = new JButton ( "OK" ) ; btn2 = new JButton ( "Cancel" ) ; container cp = getContentPane ( ) ; cp. setLayout ( new FlowLayout ( ) ) ; cp. add ( tf) ; cp. add ( btn1) ; cp. add ( btn2) ; btn1. addActionListener ( this ) ; btn2. addActionListener ( this ) ; } public void actionPerformed ( ActionEvent e) { if ( ( JButton) e. getSource ( ) == btn1) tf. setText ( "you clicked button OK" ) ; else if ( ( JButton) e. getSource ( ) == btn2) tf. setText ( "you clicked button Cancel" ) ; } public static void main ( String[ ] args) { EventDemo frame = new EventDemo ( ) ; frame. setSize ( 400 , 100 ) ; frame. setLocation ( 200 , 200 ) ; frame. setVisible ( true ) ; frame. setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; }
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】 进行投诉反馈!