public class Initial Student public static void mainString
创建和使用对象 创建“张浩”对象 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 -1 public class Student{ public void show. Info(){ return "我是一名学生"; } } 方法的返回类型为void,方法中不能有return返回值!
常见错误 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 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 void show. Info(){ return "我是一名学生"; } } 不能在方法外部直接写程序逻辑代码!
如何使用带参数的方法 3 -1 • 定义带参数的方法 public class Zhazhi{ public String zhazhi ( String fruit ) { String juice = fruit + "汁"; return juice; } 参数列表: (数据类型 参数 1,数 } 据类型 参数 2…) • 调用带参数的方法 调用方法,传递的参数要 /*调用zhazhi方法*/ 与参数列表一一对应 Zhazhi my. Zhazhi = new Zhazhi(); String my. Fruit = "苹果"; String my. Juice = my. Zhazhi. zhazhi(my. Fruit); System. out. println(my. Juice);
如何使用带参数的方法 3 -3 调用带参数的方法 实参列表 对象名. 方法名(实参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(); 实参的类型、数量、顺序 } 都要与形参一一对应
常见错误 4 -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); 形参和实参数据类型不一致!
常见错误 4 -2 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体 } //方法调用 int s=1; int e=3; boolean flag= 对象名. search. Name(s, e); 形参和实参数量不一致!
常见错误 4 -3 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体 } //方法调用 int s=1; int e=3; String name="张三"; 对象名. search. Name(s, e, name); 调用方法后没有对返回值作任何处理!
巩固练习 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, int b){ return a+b; } public int length(int a, 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
制造对象与使用对象 • 示例 – Scanner in = new Scanner(System. in); – double a = Double. parse. Double(in. next()); – double a = in. next. Double(); System. out. println()? 25
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方法和属性 26
小测试:下面两段代码的输出? • 代码一 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()); 31
对象的初始化 • 定义时初始化 – private int value=10; – private String name = "James Goslin"; – private String[] array = new String[10]; 32
1、如何使用封装 class Student { private float java = 0; 1 private float database=0; private float html=0; public float get. Java() { return java; 2 } public void set. Java (int java) { 3 if (java > 100 || java < 0) { this. java = 60; System. out. println(“Java成绩在 0和100之间,默认值是 60"); this代表 当前对象 } else this. java = java; } // 其它getter/setter方法 }
类的继承 • 子类可调用父类的方法和变量 • 子类可增加父类中没有的方法和变量 class Vehicle { String brand; void set. B(String s) { brand = s; } void show. B() { System. out. println(brand); } } Bus b = new Bus(); b. set. B(“audi”); b. set. L(710); b. show. B(); //** b. show. L(); class Bus extends Vehicle { int line; 子类继承父类的方法和变量,则这些方法和变量就属于子类, void set. L(int l) { line = l; } 则子类对象对这些方法和变量的调用是显而易见的 void show. L() { System. out. println(line); } } 61
class A { B b = new B(); int类的继承 i=256, j =64; 256 x static int k = 32; b. show(); 64 32 final float e = 2. 718 f; b. show. A(); 子类可重新定义父类中已有的变量 } • 5. 0 321 2. 718 父类中同名的变量无效(隐藏) super ? 当前对象/当前对象的父对象/其他 – 通过“super. 变量名” 和父类名. 变量名(static变量)引用 – class B extends A { public char j=‘x’; final double k =5; static int e =321; void show() { System. out. println(i + “ “ + j + “ “ + k + “ “ + e); } void show. A() { System. out. println(super. j + “ “ + A. k + “ “ + super. e); } } this. 变量名 this. 方法名 this() super. 变量名 super. 方法名 super() 62
class Engine { public void start() {} 2、继承与组合 public void rev(){} public void stop() {} } class Wheel { public void inflate(int i) {} } class Door { public Window window = new Window(); public void open(){} public void close(){} } public class Car { public Engine engine = new Engine(); public Wheel[] wheel = new Wheel[4]; public Door left = new Door(); public Door right = new Door(); public Car() { 组合: 有一个 (has-a) for (int i =0; i < 4; i++) wheel[i] = new Wheel(); 继承: 是一个 } (is-a) public static void main(String args[]) { Car car = new Car(); car. left. window. rollup(); car. wheel[0]. inflate(72); } } class Window { public void rollup() {} public void rolldown() {} } 64
class Art { Art() { 2、继承中的构造方法 System. out. println("Art Constructor"); } } class Drawing extends Art { Drawing() { { /*Drawing() System. out. println("Drawing Constructor"); } }*/ } public class Cartoon extends Drawing { Cartoon() { System. out. println("Cartoon Constructor"); super(); } System. out. println("Cartoon Constructor"); public static void main(String args[]) { } c = main(String new Cartoon(); public. Cartoon static void args[]) { } Cartoon c = new Cartoon(); } } } Art Constructor Drawing Constructor Cartoon Constructor 子类的构造方法 必须调用 父类的构造方法 65
class Game { Game(int i) { 类的继承 System. out. println(“Game Constructor"); } } • 再谈继承中的构造方法 class Board. Game extends Game { Board. Game(int i) { super(i); System. out. println(“Board. Game Constructor"); } }public class Chess extends Board. Game { Chess() { super(3); System. out. println("Cartoon Constructor"); } public static void main(String args[]) { Chess c = new Chess(); } } 子类的构造方法 必须要对父类的 构造方法进行 调用, 不管以任何形式, 否则编译出错 66
package points; public类的继承 class Point { int x, y; public void move(int dx, int dy) { x += dx; y += dy; } } • 再谈子类对父类变量的继承 package points; 目录结构 public class Point 3 d extends Point { /points/Point. class int z; /points/Point 3 d. class public void move(int dx, int dy, int dz) { /Point 4 d. class x += dx; y += dy; z += dz; } 继承与类成员的访问修饰符有关! } import points. Point 3 d; class Point 4 d extends Point 3 d { { intint w; w; public void move(int dx, intint dy, intint dz, intint dw) { { x += super. move(dx, dx; y += dy; dy, z += dz); dz; ww +=+= dw; } } 编译时报错: x, y, z仅在本包 中才能访问 67
3. 1 向上转型(upcasting) public class Shape{ void draw(){} static void start(Shape s){ s. draw(); } public static void main(String [] agrs){ start(new Circle()); start(new Square()); } } class Circle extends Shape{ void draw(){System. out. println(“draw Circle”); } } class Square extends Shape{ void draw(){System. out. println(“draw Square”); } }
3. 3 构造器中多态方法的行为 public class Shape 3{ Shape 3(){ System. out. println(“this is a shape”); draw(); } void draw(){} public static void main(String [] agrs){ Shape 3 circle = new Circle(); } } class Circle extends Shape 3{ int i = 100; Circle(){ draw(); } void draw(){System. out. println(“draw Circle” + i); } } 最后的输出结果是: this is a shape draw Circle 0 draw Circle 100
3. 4 向下转型 class A { public void f() {}; public void g() {}; } class B extends A{ public void f() {}; public void g() {}; public void h() {}; } class C extends A{ public void f() {}; public void g() {}; public void u() {}; } class D extends A{ public void f() {}; public void g() {}; } public class test. Rtti { public static void main(String[] args) { A a 1 = new A(); A a 2 = new B(); A a 3 = new C(); a 1. f(); a 2. g(); a 3. g(); } //a 2. h(); (B)a 2. h(); //向下转型 (C)a 2. h(); //异常 }
接口 (interface) • 接口名修饰 – – • public: 无任何访问限制 无修饰: 仅限于本包中 接口变量默认都是“public static final” public interface Months { int JANUARY=1, FEBRUARY=2, MARCH=3, APRIL=4, MAY=5, JUNE=6, JULY=7, AUGUST=8, SEPTEMBER=9, OCTOBER=10, NOVEMBER=11, DECEMBER=12; } 82
4、接口方法 class Circle implements Figure { interface Figure { double x, y, r; Ø half=0. 5, pi=3. 14159; 无修饰 double Circle(double u, double v, double m) void parameter(); { x=u; y=v; r=m; } Ø 但在实现接口方法的类中,修饰符为public 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(); 83 }
- Slides: 84