Java lang Threadrun class My Thread extends Thread

  • Slides: 101
Download presentation

线程的创建和启动 • 两种方法来创建线程 – 继承Java. lang. Thread类,并覆盖run() 方法 class My. Thread extends Thread {

线程的创建和启动 • 两种方法来创建线程 – 继承Java. lang. Thread类,并覆盖run() 方法 class My. Thread extends Thread { public void run( ) { /* 覆盖该方法*/ } } – 实现Java. lang. Runnable接口,并实现run() 方法 class My. Thread implements Runnable{ public void run( ) { /* 实现该方法*/ } }

线程的创建和启动 • 继承Java. lang. Thread类 public class Thread. Demo 1 { public static void

线程的创建和启动 • 继承Java. lang. Thread类 public class Thread. Demo 1 { public static void main(String args[]) { My. Thread 1 t = new My. Thread 1(); t. start(); while (true) { System. out. println("兔子领先了,别骄傲"); } } } class My. Thread 1 extends Thread { public void run() { while (true) { Java程序启动时,会立刻创建主线程, System. out. println("乌龟领先了,加油"); main就是在这个线程上运行。当不产生新 } 线程时,程序是单线程的 } } 演示示例:创建和启动多线程

线程的创建和启动 • 实现Java. lang. Runnable接口 public class Thread. Demo 2 { public static void

线程的创建和启动 • 实现Java. lang. Runnable接口 public class Thread. Demo 2 { public static void main(String args[]) { My. Thread 2 mt = new My. Thread 2(); Thread t = new Thread(mt); t. start(); while (true) { System. out. println("兔子领先了,加油"); } } } class My. Thread 2 implements Runnable { public void run() { while (true) { System. out. println("乌龟超过了,再接再厉"); } } } 演示示例:创建和启动多线程

Applet程序

Applet程序

1、Applet执行流程 import java. applet. *; public 类名 extends Applet{ public void init(){…;} public void

1、Applet执行流程 import java. applet. *; public 类名 extends Applet{ public void init(){…;} public void start(){…;} public void stop(){…;} public void destroy(){…;} public void paint(Graphics g){…;} } 4个生 命周期

1 java语言概述-编译和运行 n 如何编译Hello. World. java? javac Hello. World. java 生成. class文件 n 如何运行

1 java语言概述-编译和运行 n 如何编译Hello. World. java? javac Hello. World. java 生成. class文件 n 如何运行 java Hello. World

2、定义类及语法基础-标识符命名 例:point 4、 5 w、 A%、 this. Picture、 $current. Value、OK、_23 b、Y_123、 #length、a+b、if、变量 3、True、m-1603 错误:

2、定义类及语法基础-标识符命名 例:point 4、 5 w、 A%、 this. Picture、 $current. Value、OK、_23 b、Y_123、 #length、a+b、if、变量 3、True、m-1603 错误: 5 w、 A%、#length、a+b、if、m-1603

2、定义类及语法基础-类型转换 n 自动类型转换 低--------------------------->高 byte, short, char—> int —> long—> float —> double 操作数

2、定义类及语法基础-类型转换 n 自动类型转换 低--------------------------->高 byte, short, char—> int —> long—> float —> double 操作数 1类型 byte、short、char、int、long、float 操作数 2类型 int long float double 转换后的类型 int long float double System. out. println(‘a’+1)与System. out. println("a"+1) 的区别。 System. out. println(1+2+"34+30="+34+30);

2、定义类及语法基础-基本数据类型转换为字符串 数字转换为字符串 public static String value. Of(int n) public static String value. Of(float n)

2、定义类及语法基础-基本数据类型转换为字符串 数字转换为字符串 public static String value. Of(int n) public static String value. Of(float n) 例如: String str=String. value. Of(123. 56);

2、定义类及语法基础-字符串转换为基本数据类型 • 使用java. lang包中的Byte、Short、Integer 、 Long、 Float、Double类调相应的类方法: public static public static byte parse. Byte(String

2、定义类及语法基础-字符串转换为基本数据类型 • 使用java. lang包中的Byte、Short、Integer 、 Long、 Float、Double类调相应的类方法: public static public static byte parse. Byte(String s) throws Number. Format. Exception short parse. Short(String s) throws Number. Format. Exception short parse. Int(String s) throws Number. Format. Exception long parse. Long(String s) throws Number. Format. Exception float parse. Float(String s) throws Number. Format. Exception double parse. Double(String s) throws Number. Format. Exception 例如: double a = Double. parse. Double("1. 32"); int b = Integer. parse. Int("12");

2、定义类及语法基础-集合 Collection Map List Set Hash. Set Vector Array. List Hast. Map Hashtable 53

2、定义类及语法基础-集合 Collection Map List Set Hash. Set Vector Array. List Hast. Map Hashtable 53

3、程序流程-switch (expression) { case value 1 : { statements 1; break; } …… case

3、程序流程-switch (expression) { case value 1 : { statements 1; break; } …… case value. N : { } } statements. N; break; [default : { default. Statements; }]

4、 使用对象-定义类 public class Student { 成员属性 String name; //姓名 int age; //年龄 String

4、 使用对象-定义类 public class Student { 成员属性 String name; //姓名 int age; //年龄 String class. No; //班级 String hobby; //爱好 成员方法 //输出信息方法 public void show(){ System. out. println(name + "n年龄:" + age + "n就读于:" + class. No + "n爱好:" + hobby); } }

4、 使用对象-创建和使用对象 创建“张浩”对象 public class Initial. Student { 创建对象 public static void main(String args[]){

4、 使用对象-创建和使用对象 创建“张浩”对象 public class Initial. Student { 创建对象 public static void main(String args[]){ Student student = new Student(); student. name = "张浩"; 给每个属性赋值 student. age = 10; student. class. No = "S 1班"; student. hobby = "篮球"; 调用方法 student. show(); } }

4、 使用对象-测试:下面两段代码的输出? • 代码一 int a; int b; a = 32; b = a;

4、 使用对象-测试:下面两段代码的输出? • 代码一 int a; int b; a = 32; b = a; a = a + 1; System. out. println(b); • 代码二 Person a; Person b; a = new Person("Tom"); b = a; a. change. Name("Eric"); System. out. println(b. get. Name()); 61

常见错误 4 -1 public class Student{ public void show. Info(){ return "我是一名学生"; } }

常见错误 4 -1 public class Student{ public void show. Info(){ return "我是一名学生"; } } 方法的返回类型为void,方法中不能有return返回值!

常见错误 4 -2 public class Student{ public double get. Info(){ double weight = 95.

常见错误 4 -2 public class Student{ public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight, height; } } 方法不能返回多个值!

常见错误 4 -3 public class Student{ public String show. Info(){ return "我是一名学生"; public double

常见错误 4 -3 public class Student{ public String show. Info(){ return "我是一名学生"; public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight; } } } public class Student{ public String show. Info(){ return "我是一名学生"; } public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight; } } 多个方法不能相互嵌套定义!

常见错误 4 -4 public class Student{ int age=20; if(age<20){ System. out. println("年龄不符合入学要求!"); } public

常见错误 4 -4 public class Student{ int age=20; if(age<20){ System. out. println("年龄不符合入学要求!"); } public void show. Info(){ return "我是一名学生"; } } 不能在方法外部直接写程序逻辑代码!

4、 使用对象-使用带参数的方法 调用带参数的方法 实参列表 对象名. 方法名(实参1, 实参2, ……,实参n) 先实例化对象, public static void main(String[] args)

4、 使用对象-使用带参数的方法 调用带参数的方法 实参列表 对象名. 方法名(实参1, 实参2, ……,实参n) 先实例化对象, public static void main(String[] args) { 再使用方法 Customer. Biz st = new Customer. Biz(); Scanner input = new Scanner(System. in); for(int i=0; i<5; i++){ System. out. print(“请输入客户的姓名:"); String new. Name = input. next(); st. add. Name(new. Name); } st. show. Names(); 实参的类型、数量、顺序 } 都要与形参一一对应

常见错误 3 -1 //方法定义 public boolean search. Name(int start , int end , String

常见错误 3 -1 //方法定义 public boolean search. Name(int start , int end , String name){ //方法体 } //方法调用 String s="开始"; int e=3; String name="张三"; boolean flag=对象名. search. Name(s , e , name); 形参和实参数据类型不一致!

常见错误 3 -2 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体

常见错误 3 -2 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体 } //方法调用 int s=1; int e=3; boolean flag= 对象名. search. Name(s, e); 形参和实参数量不一致!

4、 使用对象-static (静态变量/静态方法) 类的变量/方法,独立于类的对象,可以直接根据类名调用 class S { static int A = 12, B =

4、 使用对象-static (静态变量/静态方法) 类的变量/方法,独立于类的对象,可以直接根据类名调用 class S { static int A = 12, B = 34; static void print() { … } } class Test { public static void main(String args[]) { System. out. println(“A=“ + S. A + “ B=“ + S. B); S. print(); } } static方法中仅仅可以调用其他static方法 70

6、继承与多态-接口 class Circle implements Figure { interface Figure { Ø 无修饰 double half=0. 5,

6、继承与多态-接口 class Circle implements Figure { interface Figure { Ø 无修饰 double half=0. 5, pi=3. 14159; double x, y, r; Circle(double u, double v, double m) void parameter(); Ø 但在实现接口方法的类中,修饰符为public { x=u; y=v; r=m; } void area(); public void parameter() } { System. out. println(x+“ “+y+“ “+r); } class Triangle implements Figure { double b, h; Triangle (double u, double v) { b = u; h = v; } public void parameter() { System. out. println(b + “ “ + h); } public void area() { System. out. println(half*h*b); } } public void area() { System. out. println(pi*r*r); } } Triangle t = new Triangle(2, 3); Circle c = new Circle(4, 5, 6); Figure[] f = {t, c}; for (int i =0; i < f. length; i++) { f[i]. parameter(); f[i]. area(); 79 }

8、Java输入输出-字节流 system. in,类型:Input. Stream,表示终端输入字节流 Buffered. Reader buf=new Buffered. Reader( new Input. Stream. Reader(System. in));

8、Java输入输出-字节流 system. in,类型:Input. Stream,表示终端输入字节流 Buffered. Reader buf=new Buffered. Reader( new Input. Stream. Reader(System. in)); buf. readline();

9、Java GUI-事件

9、Java GUI-事件

练习 写出下面程序的运行结果。 //Examplex 2_3. java public class Example 2_3{ public static void main(String[] args){

练习 写出下面程序的运行结果。 //Examplex 2_3. java public class Example 2_3{ public static void main(String[] args){ int x = 1, a = 0, b = 0; switch(x){ case 0: b++; case 1: a++; case 2: a++, b++; } System. out. prinln(“a=”+a); System. out. prinln(“b=”+b); } }

巩固练习 1. 在以下Java程序类中,语法正确的是()。 A) public int uf. Test(int num) int sum=num+100; return sum; }

巩固练习 1. 在以下Java程序类中,语法正确的是()。 A) public int uf. Test(int num) int sum=num+100; return sum; } B) public String uf. Test(int num) int sum=num+100; return sum; } C) public void uf. Test(int num) int sum=num+100; return sum; } D) public int uf. Test(int num) int sum=num+100; }

巩固练习 1. 在Java中,类Test的源代码如下所示,改程序的编译运行结果是() 。 public class Test(){ public int add(int a, intb){ return a+b;

巩固练习 1. 在Java中,类Test的源代码如下所示,改程序的编译运行结果是() 。 public class Test(){ public int add(int a, intb){ return a+b; } public int length(inta, int b){ return add(a, b)*2; } public static void main(String[] args){ Test test=new Test(); System. out. println("结果为:“ + test. length(10, 20)); } } A)第 6行出现编译错误,提示无法找到add方法 B)第 10行出现编译错误,提示无法将int类型自动转换成String类型 C)输出:结果为: 60 D)输出:结果为:null