java初级学习总结文件.pptVIP

  • 2
  • 0
  • 约1.36万字
  • 约 77页
  • 2020-08-13 发布于湖北
  • 举报
类的组合与嵌套 类的组合(在一个类中将另一个类的对象作为自己的属性) 嵌套类 class A { static int i=10; static class B { public B() { System.out.println(static内部类访问外部类数据:+i); } } } public class Test { public static void main(String[] args) { A.B ab = new A.B(); //生成内部类B的对象 } } 多态 多态性是指同名的不同方法在程序中共存 多态实现的两种形式:覆盖实现多态性 重载实现多态性 静态变量的作用: (1)能被类的所有实例共享,可以作为实例之间进行交流的共享数据 (2)如果类的所有 实例都包含一个相同的常量属性,把这个属性定义为静态常量,在内存中只保存该变量的一个副本,节省内存空间 覆盖:子类对父类方法的覆盖(不同类的同名方法) 重载:一个类中,同名方法(参数不同) class Employee { void upSalary(float inc) { salary = salary + inc ; } void upSalary() { salary = salary * 1.1F ; } ... } class Manager extends Employee { void upSalary() { salary = salary * 1.2F ; } ... } 方法覆盖与方法重载 重载 覆盖 static-静态修饰符,可以修饰类中的属性与方法 静态属性的访问 类名.属性名 或:对象名.属性名 静态变量和静态方法 抽象类和抽象方法 抽象类 public abstract class 抽象类名 { 类体; } 抽象方法 public abstract 返回类型 方法名(形式参数列表); import java.io.*; public class MonthSwitch { public static void main(String args[ ]) throws IOException { int x; String s; BufferedReader r=new BufferedReader (new InputStreamReader(System.in)); System.out.println(”enter an integer:”); s=r.readLine(); x=Integer.parseInt(s); if(x1x12) { System.out.println(请重新输入(1~12):); s=r.readLine(); x=Integer.parseInt(s); } 纠错分析题 switch(x) { case 2: System.out.println(28天); break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(31天); break; case 4: case 6: case 9: case 11: System.out.println(31天); break; } } } 纠错分析题 for(循环变量初始化;循环条件;循环变量增量) {

文档评论(0)

1亿VIP精品文档

相关文档