Learn Java until I am Die...

master
Black-Gold 8 years ago
parent 4f352b421c
commit 7f22d5ed10

@ -0,0 +1,9 @@
public class Jb01_HelloWorld //类名与文件名保持一致由26个大小写字母、数字及_$组成。不能以数字开头,禁止使用关键字,
{
//定义主函数
public static void main(String[] args) //args即arguments
{
System.out.println("HelloWorld"); //打印输出HelloWorld,ln表示换行!
//public class等表示关键字由Java定义
}
}

@ -0,0 +1,31 @@
public class Jb02_VarDemo1
{
public static void main(String[] args)
{
/*
byte 1
boolean 1
short 2
char 2
int 4
float 4
long 8
double 8
*/
//数据类型 变量名 = 初始化值;
byte b = 1; //定义变量b,初始化值为1
short s = 2;
int i = 3;
long l = 4;
float f = 5.5f;
double d = 6.6;
char c = 'c';
boolean bo = true;
bo = false;
{
int x = 9;
System.out.println("x"); //局部变量x
}
System.out.println(c);
}
}

@ -0,0 +1,23 @@
public class Jb03_VarDemo2
{
public static void main(String[] args)
{
/*
int x = 1;
x = x + 1;
byte b = 2;
x = x + b; //自动类型转换
b = (byte)(b + 2); //强制类型转换
System.out.println(b);
*/
byte b = 1;
byte b1 = 2;
byte b2 = 3;
// b = b1 + b2; b1与b2是变量无法判断并赋值
int x;
int x1 = Integer.MAX_VALUE;
int x2 = 2;
x = x1 + x2; //int是默认类型自动强制转换
}
}

@ -0,0 +1,10 @@
public class Jb04_Operate2
{
public static void main(String[] args)
{
short s = 1;
s+=2; //自动转换 相当于s = (short)(s+2);
// s = s+2; 右边s是变量可能会出现精度丢失。
System.out.println(s);
}
}

@ -0,0 +1,23 @@
public class Jb05_Operate3
{
public static void main(String[] args)
{
//比较运算符结果只能是true或false
/*
boolean
&falsefalsetruetrue
|truetrue;falsefalse
^falsetrue
!
&&&false
|||true
*/
}
}

@ -0,0 +1,11 @@
public class Jb06_Operate4
{
public static void main(String[] args)
{
// 三元运算符:(条件表达式)?表达式1:表达式2;
// 如果条件表达式结果是true结果是表达式1。否则结果是表达式2。
int x = 1,y;
y = (x>3)?21:27;
System.out.println(y);
}
}

@ -0,0 +1,28 @@
public class Jb07_IfDemo
{
public static void main(String[] args)
{
// 判断结构:
// 当if else运算后由具体结果时可简化成三元运算符
int x = 1;
if (x>3)
{
System.out.println("yeah");
}
else if (x>2)
{
System.out.println("OK");
}
else
{
System.out.pintln("欧了");
}
//局部代码块可定义局部变量的生命周期。
{
int y = 3;
}
System.out.println("over"+y); //y在局部代码块里会出现错误。
}
}

@ -0,0 +1,42 @@
public class Jb08_IfTest
{
public static void main(String[] args)
{
// 需求:用户输入具体数值,判断出对应的星期
int week = 3;
if (week==1)
{
System.out.println(week+"是星期一");
}
else if (week==2)
{
System.out.println(week+"是星期二");
}
else if (week==3)
{
System.out.println(week+"是星期三");
}else
{
System.out.println("输入错误请输入1-3的数字");
}
// 需求:根据用户输入数值,输出对应的季度。
int month = 3;
if (month<1 || month>12)
{
System.out.println("没有此季节");
}
else if (month>=3 && month<=5)
{
System.out.println(month+"月是春季");
}
else if (month>=6 && month<=8)
{
System.out.println(month+"月是夏季");
}
else
{
System.out.println(month+"月是冬季");
}
}
}

@ -0,0 +1,45 @@
public class Jb09_SwitchDemo
{
public static void main(String[] args)
{
// 选择结构Switch
/*
switch ()
{
case 1:
;
break;
case 2:
2;
break;
......
default:
;
break;
}
*/
//根据输入数字,输出对应季节
int month = 3;
switch (month)
{
case 3:
case 4:
case 5:
System.out.println("是春季");
break;
default:
System.out.println("没有对应的季节");
break; //此处break可省略
}
/*
ifswitch
if:
1
2
3boolean
switch
1
2switch
*/
}
}

@ -0,0 +1,45 @@
public class Jb10_WhileDemo
{
public static void main(String[] args)
{
// 循环结构
/*
while
while ()
{
;
}
do
{
;
}
while ();
do while
*/
/*
int x = 1;
do
{
System.out.println("x="+x);
x++;
}
while (x<2);
int y = 1;
while (y<2)
{
System.out.println("y="+y);
}
*/
//获取1到100数字的和:————累加思想
int x = 1;
int sum = 0;
while (x<=100)
{
sum+=x;
x++;
}
System.out.println("sum="+sum);
}
}

@ -0,0 +1,18 @@
public class Jb11_WhileTest
{
public static void main(String[] args)
{
//输出1到100之间里2的倍数有几个。计数器思想
int x = 1;
int count = 0;
while (x<=100)
{
if (x%2==0)
{
count++;
}
x++;
}
System.out.println("1~100之间2的倍数个数总共是"+count);
}
}

@ -0,0 +1,23 @@
public class Jb12_ForDemo
{
public static void main(String[] args)
{
/*
for (;;)
{
;()
}
whilefor
for
while使使
*/
//for完成累加
int sum = 0;
for (int x=1; x<=100; x++)
{
sum+=x;
}
System.out.println(sum);
//System.out.println(x); 此处会提示找不到变量x错误因x已在内存中释放
}
}

@ -0,0 +1,51 @@
public class Jb13_ForTest
{
public static void main(String[] args)
{
/*,
for (int x=1;x<=4; x++)
{
for (int y=1;y<=4 ;y++ )
{
System.out.print("*");
}
System.out.println("*");
}
*/
/*
//int z = 5;
for (int x=1;x<=5; x++) //1-5 1-4 1-3 //1-5 2-5 3-5
{
for (int y=x;y<=5 ;y++ )
{
System.out.print("*");
}
System.out.println("*");
//z--;
}
*/
/*
for (int x=1;x<=5; x++)
{
for (int y=5;y>=x ;y-- )
{
System.out.print(y);
}
System.out.println();
}
*/
/*99 \n: \t \b退 \r
for (int x=1;x<=9 ;x++ )
{
for (int y=1;y<=x ;y++ )
{
System.out.print(y+"*"+x+"="+x*y+"\t");
}
System.out.println();
}
*/
}
}

@ -0,0 +1,21 @@
public class Jb14_Function
{
public static void main(String[] args)
{
/*
( 1, 2,.......)
{
;
return;
}
*/
//定义一个功能,求两数之和
int sum = add(6,2);
System.out.println(sum);
}
public static int add(int a,int b)
{
int sum = a+b;
return sum;
}
}

@ -0,0 +1,41 @@
public class Jb15_Function2
{
public static void main(String[] args)
{
//draw(7,8);
getLevel(99);
}
/*
public static void draw(int row,int col)
{
for (int x=1;x<=row ;x++ )
{
for (int y=1;y<=col ;y++ )
{
System.out.print("*");
}
System.out.println();
}
return; //此处return可省略即返回值具体类型不确定使用void时可省略return
}
*/
//根据分数输出对应等级
public static void getLevel(int grade)
{
if (grade<0 || grade>100)
{
System.out.println("错误");
}
else if (grade>=90 && grade<=100)
{
System.out.println("A");
}
else if (grade>=60 && grade<=89)
{
System.out.println("B");
}
else
System.out.println("C");
return;
}
}

@ -0,0 +1,18 @@
public class Jb16_Function3
{
public static void main(String[] args)
{
/*
*/
}
//两小数之和
public static double add(double a,double b)
{
return a+b;
}
//三整数之和
public static double add(int a,int b,int c)
{
return a+b+c;
}
}

@ -0,0 +1,34 @@
public class Jb17_ArrDemo
{
public static void main(String[] args)
{
/*1
[] = new [];
2
[] = new [] {,,........};
0
*/
//int [] arr = new int[]{2,54,4,5,4,4,5,79};
int [] arr = {-2,-5,-79,-9};
for (int x=0;x<arr.length ; x++)
{
System.out.println("arr["+x+"] = "+arr[x]);
}
int max = getMax(arr);
System.out.println("最大值是"+max);
}
//定义功能求数组最大值
public static int getMax(int[] arr)
{
int max = arr[0];
for (int x=1 ;x<arr.length ;x++ )
{
if (arr[x]>max)
{
max = arr[x];
}
}
return max;
}
}

@ -0,0 +1,58 @@
import java.util.*;
public class Jb18_ArrSort
{
public static void main(String[] args)
{
int[] arr = {71,15,4,50,54,55,5,45,454,496,78,};
ergodic(arr);
//selectSort(arr); //调用选择排序
//bubbleSort(arr); //调用冒泡排序
Arrays.sort(arr); //调用Java定义好的排序功能
ergodic(arr);
}
//遍历数组
public static void ergodic(int[] arr)
{
for (int x=0; x<arr.length; x++)
{
if (x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.println(arr[x]);
}
}
/*
public static void selectSort(int[] arr)
{
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=x+1;y<arr.length ; y++)
{
if (arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
*/
//冒泡排序
public static void bubbleSort(int[] arr)
{
for (int x=0; x<arr.length-1; x++)
{
for (int y=0;y<arr.length-x-1 ;y++ ) //-x代表
{
if (arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
}

@ -0,0 +1,59 @@
public class Jb19_ArrSearch
{
public static void main(String[] args)
{
int[] arr = {0,1,2,3,4,5,6,7,8,9};
int index = binarySearch_2(arr,7);
System.out.println(index);
}
/*
public static int getIndex(int[] arr,int key)
{
for (int x=0;x<arr.length ;x++ )
{
if (arr[x]==key)
{
return x;
}
}
return -1;
}
*/
//二分查找法
public static int binarySearch(int arr[],int key)
{
int max,min,mid;
min = 0;
max = arr.length-1;
mid = (max+min)/2;
while (arr[mid]!=key)
{
if (key>arr[mid])
min = mid + 1;
else if (key<arr[mid])
max = mid - 1;
if (max<min)
return -1;
mid = (max+min)/2;
}
return mid;
}
//第二种写法
public static int binarySearch_2(int[] arr,int key)
{
int max,mid,min;
min = 0;
max = arr.length-1;
while (min<=max)
{
mid = (max+min)>>1; //右移一位相当于除以2的一次幂
if(key>arr[mid])
min = mid + 1;
else if(key<arr[mid])
max = mid - 1;
else
return mid;
}
return -1;
}
}

@ -0,0 +1,65 @@
public class Jb20_Object
{
public static void main(String[] args)
{
Car benz = new Car(); //通过new关键字创建一个Car的实例
benz.wheel = 4;
benz.color = "green";
benz.run(); //调用对象中的run功能
Car gtr1 = new Car();
Car gtr2 = new Car();
show(gtr1);
show(gtr2);
}
/*
Car benz = new Car();
benz.run();
new Car().run();
Car gtr1 = new Car();
show(gtr1);
show(new new Car());
*/
public static void show(Car car) //类类型的变量一定指向对象否则就为null
{
car.wheel = 6;
car.color = "red";
System.out.println(car.wheel+"\t"+car.color);
}
}
class Car
{
int wheel; //int wheel = 4;显示初始化值为4
String color;
void run()
{
//int num = 6; 局部变量打印num为6
System.out.println(wheel+"\t"+color);
}
}
/*
>>:>>:
访
*/

@ -0,0 +1,30 @@
public class Jb21_DataTransmit
{
//基本数据类型参数传递
public static void main(String[] args)
{
int x = 1;
show(x);
System.out.println(x); //输出1
}
public static void show(int x)
{
x = 2;
}
}
//引用数据类型参数传递
class Demo
{
int x = 1;
public static void main(String[] args)
{
Demo data = new Demo();
data.x = 3;
show(data);
System.out.println(data.x); //输出为2
}
public static void show(Demo data)
{
data.x = 2;
}
}

@ -0,0 +1,28 @@
public class Jb22_EncampsObj
{
//面向对象的封装
public static void main(String[] args)
{
Person xiaoMing = new Person();
xiaoMing.getAge(21);
xiaoMing.speak();
}
}
class Person
{
private int age;
public void getAge(int num)
{
if(num>=0 && num<=150)
{
age = num;
}
else
System.out.println("错误输出年龄范围在0~150");
}
void speak()
{
System.out.println("年龄是"+age);
}
}

@ -0,0 +1,76 @@
/**
@author XiaoMing
@version V1.0
*/
public class Jb23_ArrTool
{
/*
*/
private Jb23_ArrTool(){}
/**
@param arr int
@return */
public static int getMax(int[] arr)
{
int maxIndex = 0;
for (int x=1;x<arr.length ;x++ )
{
if (arr[x]>arr[maxIndex])
{
arr[maxIndex] = arr[x];
}
}
return arr[maxIndex];
}
//数组选择排序
public static void selectSort(int[] arr)
{
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=x+1; y<arr.length; y++)
{
if (arr[x]>arr[y])
{
swap(arr,x,y);
}
}
}
}
//数组元素的位置换位
public static void swap (int[] arr,int x,int y)
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
//获取指定元素在指定数组的角标,不存在返回-1
public static int getIndex(int[] arr,int key)
{
for (int x=0;x<arr.length ;x++ )
{
if (arr[x]==key)
{
return x;
}
}
return -1;
}
//将int数组转换成字符串
public static String arrayToString(int[] arr)
{
String s = "";
for (int x=0; x<arr.length;x++ )
{
if (x!=arr.length-1)
s = s + arr[x]+",";
else
s = s + arr[x];
}
return s;
}
}

@ -0,0 +1,12 @@
public class Jb23_ArrToolDemo
{
public static void main(String[] args)
{
int[] arr = {0,1,32,5,8,4,9};
int index = Jb23_ArrTool.getIndex(arr,43); //获取数字43在数组中的角标
System.out.println(index);
int max = getMax(arr);
System.out.println("数组最大值是"+max);
}
}

@ -0,0 +1,20 @@
public class Jb24_Single
{
//单例设计模式
//解决问题:确保一个类在内存中的对象唯一性
public static void main(String[] args)
{
//Single sDemo = Single.getInstance();
Single sDemo = Single.s; //等同于Single sDemo = Single.getInstance();
}
}
class Single
{
static Single s = new single();
private Single(){};
public static Single getInstance()
{
return s;
}
}

@ -0,0 +1,29 @@
public class Jb25_ObjExtends
{
public static void main(String[] args)
{
Student a = new Student();
a.name = "xiaoming";
a.age = 17;
a.study();
}
}
/*
:
*/
class Person
{
String name;
int age;
}
class Student extends Person
{
void study()
{
System.out.println(age+"岁的"+name+"正在学习");
}
}

@ -0,0 +1,34 @@
public class Jb26_Final
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
//继承的弊端:打破了封装性,会覆盖父类方法
/*
final
访final
_
*/
class Fu
{
void method()
{
//调用底层系统资源
}
}
class Zi extends Fu
{
void method()
{
// final int x = 1;
// x = 2; 此会提示出错变量x为最终变量无法赋值
System.out.println("OK");
}
}

@ -0,0 +1,56 @@
public class Jb27_AbstractClass
{
public static void main(String[] args)
{
/*
abstract
abstract
AWT
class Demo()
{
void show1()
{}
}
private
static
final
*/
System.out.println("Hello World!");
}
}
abstract class Demo
{
abstract void show();
//将DemoA和DemoB向上抽取共性方法但是内容不同具体不清楚此时用abstract修饰
}
class DemoA
{
void show()
{
System.out.println("show A");
}
}
class DemoB
{
void show()
{
System.out.println("show B");
}
}

@ -0,0 +1,48 @@
public class Jb28_AbstractTest
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
abstract class Staff
{
private String name;
private String id;
private double pay;
Staff(String name,String id,double pay)
{
this.name = name; //³õʼ»¯
this.id = id;
this.pay = pay;
}
public abstract void work();
}
class Programmer extends Staff
{
Programmer(String name,String id,double pay)
{
super(name,id,pay);
}
public void work()
{
System.out.println("´ò´úÂë");
}
}
class Manager extends Staff
{
private int bonus;
Manager(String name,String id,double pay,int bonus)
{
super(name,id,pay);
this.bonus = bonus;
}
public void work()
{
System.out.println("¹ÜÀíÈËÔ±");
}
}

@ -0,0 +1,88 @@
/*
abstract class Demo
{
abstract void show1();
abstract void show2();
}
interface
public static final
public abstract
public
*/
interface Demo
{
public static final int NUM = 4;
public abstract void show1();
public abstract void show2();
}
/*
*/
class DemoImpl implements Demo
{
public void show1()
{}
public void show2()
{}
}
/*
Java
*/
interface A
{
public void show1();
}
interface B
{
public void show2();
}
class Test implements A,B //多实现
{
public void show()
{
System.out.println("OK");
}
}
//一个类继承另一个类的同时,还可实现多个接口
class C
{
public void method()
{}
}
abstract class Test2 extends C implements A,B
{
}
public class Jb29_Interface
{
public static void main(String[] args)
{
/*DemoImpl inf = new DemoImpl();
System.out.println(inf.NUM);
System.out.println(DemoImpl.NUM);
System.out.println(Demo.NUM);*/
}
}
/*
使
*/

@ -0,0 +1,31 @@
public class Jb30_Interface2
{
//实现电脑USB接口功能
public static void main(String[] args)
{
useUSB(new Upan());
}
public static void useUSB(USB u) //接口类型的引用,用于接收(指向)接口的子类对象
{
u.open();
u.close();
}
}
interface USB
{
public void open();
public void close();
}
class Upan implements USB
{
public void open()
{
System.out.println("开启");
}
public void close()
{
System.out.println("关闭");
}
}

@ -0,0 +1,76 @@
public class Jb31_ObjPol
{
//对象的多态性;代码中的体现就是父类或接口的引用指向其子类的对象
public static void main(String[] args)
{
/*().访
访*/
Animal a = new Cat();
a.eat();
//如果仍要使用猫的特用功能,可将对象向下转型
//自始至终都是子类对象进行类型的变换
Cat c = (Cat)a; //向下转型目的是为了使用子类的特用方法
c.eat();
c.catch();
/*
Cat c = new Cat();
//Dog d = new Dog();
//c.eat();
method(c);
method(new Dog());
*/
method(new Cat());
}
/*使
使
使
*/
public static void method(Animal a)
{
a.eat();
if (a instanceof Cat)
//instanceof 用于判断对象的具体类型,只能用于引用数据类型判断
//通常在向下转型前用于健壮性的判断
{
Cat c2 = (Cat)a;
c2.catch();
}
else if (a instanceof Dog)
{
Dog d2 = (Dog)a;
d2.watchHome();
}
}
}
abstract class Animal
{
abstract void eat();
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃屎");
}
public void watchHome()
{
System.out.println("看家");
}
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void catch()
{
System.out.println("捉老鼠");
}
}

@ -0,0 +1,54 @@
public class Jb32_PolMember
{
/*
Fu f = new Zi();
()
*/
public static void main(String[] args)
{
Fu f = new Zi();
f.method(); //可以不需要对象来调用等同于直接用类名调用Fu.method();,
//f.show();
//System.out.println(f.num);
}
}
class Fu
{
int num = 1;
void show()
{
System.out.println("FU");
}
static void method()
{
System.out.println("FU static");
}
}
class Zi extends Fu
{
int num = 2;
void show()
{
System.out.println("ZI");
}
static void method()
{
System.out.println("ZI static");
}
}

@ -0,0 +1,54 @@
public class Jb33_InnerClass
{
public static void main(String[] args)
{
//Outer out = new Outer();
//out.method();
//直接访问外部类中的内部类中的成员,内部类共有时
//Outer.Inner in = new Outer().new Inner();
//in.show();
//如果内部类是静态的,相当于一个外部类
//Ouer.Inner in = new Outer.Inner();
//in.show();
//如果内部类是静态的,成员是静态的
Ouer.Inner.fun();
}
}
/*
访
访
访
*/
class Outer
{
private static int num = 1;
class Inner //内部类
{
void show()
{
System.out.println("嘿嘿"+num);
}
/*
static void fun() //如果内部类中定义了静态方法,该内部类必须是静态的
{
System.out.println("fun 嘿嘿"+num);
}
*/
}
public void method() //外部类访问内部类方法
{
Inner in = new Inner();
in.show();
}
}
/*
访
*/

@ -0,0 +1,53 @@
public class Jb34_InnerClass2
{
public static void main(String[] args)
{
new Outer().method();
new Outer2().method();
}
}
/*
访
.this
*/
class Outer
{
int num = 1;
class Inner
{
int num = 2;
void show()
{
int num = 3;
System.out.println(num); //打印show方法内的成员
System.out.println(this.num); //打印内部中的成员
System.out.println(Outer.this.num); //打印外部类中的成员
}
}
void method()
{
new Inner().show();
}
}
//JDK1.7之前内部类在局部位置上只能访问局部中被final修饰的局部变量
//JDK1.8时,内部类在局部位置上可以直接访问局部变量
class Outer2
{
int num = 1;
void method()
{
int x = 2; //局部变量x
class Inner2
{
void show()
{
System.out.println("show"+x);
}
}
Inner2 in = new Inner2();
in.show();
}
}

@ -0,0 +1,39 @@
public class Jb35_AnsInnerClass
{
/*使
*/
public static void main(String[] args)
{
new Outer().method();
}
}
class Outer
{
/*
class Inner extends Demo
{
void show()
{
System.out.println("呵呵"+num);
}
}
*/
public void method()
{
//new Inner().show();
new Demo()
{
void show()
{
System.out.println("呵呵呵呵"+num);
}
}.show();
}
}
abstract class Demo
{
abstract void show();
}

@ -0,0 +1,46 @@
public class Jb36_AnsInnerClass2
{
/*
使
*/
public static void main(String[] args)
{
show(new Inf()
{
public void show1()
{}
public void show2()
{}
});
}
public static void show(Inf in)
{
in.show1();
in.show2();
}
}
interface Inf
{
void show1();
void show2();
}
//匿名内部类的使用
class Outer
{
public void method()
{
Inf in = new Inf()
{
public void show1()
{}
public void show2()
{}
};
in.show1();
in.show2();
}
}

@ -0,0 +1,78 @@
public class Jb37_ObjExcep
{
/*
Throwable:ErrorException
:
try
{
}
catch ( )
{
}
finally
{
}
*/
public static void main(String[] args) //声明throws MinusIndexException
{
int [] arr = new int[3];
Demo d = new Demo();
try
{
int num = d.method(arr,-27);
System.out.println("Hello World!"+num);
}
catch (MinusIndexException m) //多个catch要把父类exception放在最后
{
m.printStackTrace();//JVM默认的异常处理机制
System.out.println("角标不为负");
//System.exit(0);//退出JVM
}
finally //通常用于释放资源
{
System.out.println("finally");
}
}
}
class Demo
{
public int method (int[] arr,int index)throws MinusIndexException //声明:
{
if (arr==null)
{
throw new NullPointerException("数组引用不能为空");
}
if (index>=arr.length)
{
throw new ArrayIndexOutOfBoundsException("角标越界"+index);
}
if (index<0)
{
throw new MinusIndexException();
}
return arr[index];
}
}
/*
throwsthrow
*/
class MinusIndexException extends Exception
{
MinusIndexException()
{
}
MinusIndexException(String msg)
{
super(msg);
}
}

@ -0,0 +1,50 @@
public class Jb38_ObjClass
{
public static void main(String[] args)
{
Dog d1 = new Dog("daHei");
Dog d2 = new Dog("daHei");
Dog d3 = d1;
Demo d = new Demo();
System.out.println(d1==d2);
System.out.println(d1.equals(d2));
//System.out.println(d1.equals(d));
System.out.println(d1);
System.out.println(d1.hashCode());
Class c1 = d1.getClass();
Class c2 = d2.getClass();
System.out.println(c1==c2);
System.out.println(d1.getClass().getName());
}
}
class Dog extends Object
{
private String name;
Dog(String name)
{
this.name = name;
}
/*
*/
public boolean equals(Object obj)
{
if (!(obj instanceof Dog))
{
throw new ClassCastException("类型与定义不同,错误");
}
Dog d = (Dog)obj;
return this.name == d.name;
}
public String toString()
{
return "Dog:"+name;
}
}
class Demo
{
}

@ -0,0 +1,55 @@
public class Jb39_Multithread
{
/*
线
线
线Thread
1Thread
2Threadrun
3Thread线
4start线run
*/
public static void main(String[] args)
{
Thread t1 = new Thread();
Thread t2 = new Thread();
Demo d1 = new Demo("线程1");
Demo d2 = new Demo("线程......二");
d1.start(); //开启线程1,调用run方法
d2.start();
System.out.println("Hello World!"); //线程三
}
}
class Demo extends Thread
{
private String name;
Demo(String name)
{
this.name = name;
}
public void run()
{
for (int x=0;x<9 ;x++)
{
for (int y=0; y<27; y++)
{
}
System.out.println(name+Thread.currentThread().getName()+"..."+x); //通过Thread的getName方法获取对象的名称
}
}
/* public void show()
{
for (int x=0;x<10 ;x++ )
{
for (int y=0;y<100 ;y++ )
{
System.out.println(name+"x="+x);
}
}
}*/
}
Loading…
Cancel
Save