面向对象第五天(Java)
主要内容:
代码编写中的一些基本知识
外部类与内部类
1、包:允许类组成较小的单元,易于找到和使用相应的文件
防止命名冲突
更好的保护类,属性和方法。
package com.hwadee.ttip;
包命名规范:
包名由小写字母组成,不能以圆点开头和结尾。
包名之前最好加上唯一的前缀,通常使用组织机构倒置的网络域名, 如:www.edu.cn package cn.edu.mypackage;
包名后续部分依不同机构内部的规范不同而不同
package cn.edu.javagroup.powerproject;
为了使用不再同一包中的类,需要在java程序中使用import关键字导入这个类import java.util.*;和import cn.edu.ttip.UserService;
语法:import 包名.类名;
2、final和static
(1)final
①final表示“最终”
final在类之前:表示该类不能被继承
final在方法之前:表示该方法不能被覆盖
final在变量之前:定义一个常量
②final类
public final class FinalClass {
public final double PI=3.1415 ;
}
③final方法:
public class FinalMethod {
public final double Division() {
return 1.0 ;
}
}
class SubFinalMethod extends FinalMethod {
}
④final类和abstract类:abstract类不能直接产生属于这个类的对象,只有创建子类后才可以实例化。final类不能被其他任何类所继承。
(2)static
static在变量或方法之前,表明他们属于类范围的,称为类方法(静态方法)或类变量(静态变量)
若变量或方法之前无static修饰,则是实例方法和实例变量
①静态属性:
类变量在各个实例间共享
类变量的生存期不依赖于对象,相当于c语言中的全局变量的作用,其他类可以不通过实例化访问他们。
public class StaticVar {
public static int number=3 ;
}
public class OtherClass {
public void method () {
int x=StaticVar.number;
}
}
②静态方法:
类方法相当于C语言中的全局函数,其他类不用实例化即可调用他们。
public class GeneralFunction {
public static int addUp (int x,int y) {
return x+y;
}
}
public class UseGeneral {
public void method () {
int a=9 ;
int b=10 ;
int c=GeneralFunction.addUp(a, b);
}
}
③静态变量/方法和实例变量/方法
静态变量/方法都是使用类名引用
实例变量/方法必须先创建实例对象,然后使用实例对象引用
public class StaticAndInstanceDemo {
private int id=1 ;
public static int number=3 ;
public int getId () {
return this .id;
}
public static int addUp (int x,int y) {
return x+y;
}
}
④完整例子
public class StaticClass {
static {
System.out .println("static block 2" );
}
static {
System.out .println("static block 1" );
}
public static final double PI=3.1415 ;
private int i;
public StaticClass () {
System.out .println("构造方法被调用" );
}
public static void method1 () {
System.out .println("静态方法被调用" );
}
public static void main (String[] args) {
System.out .println("StaticClass.PI:" +StaticClass.PI);
StaticClass.method1();
}
}
3、内部类:
在面向对象程序设计中,可以在一个类的内部定义另一个类,即内部类;
内部类有两种类型:静态内部类、非静态内部类(普通内部类),静态内部类使用很少,最重要的是普通内部类
使用内部类可以隐藏实现细节,也可以让代码更加简洁。
(1)普通内部类:
内部类几乎可以处于内部的任意位置,可以与实例变量处于同一级,或处于方法之类,甚至是一个表达式的一部分。
普通内部类和平时的类定义没有什么不同,主要是内部类可以访问外部类的属性和方法,这里的语法和平时有些不同。
public class Forest {
private int sum_tree=1000 ;
private int type_tree=100 ;
public int getTreeType () {
return type_tree;
}
class MagicalAnimal{
private String name;
public void setName (String name) {
this .name=name;
}
public int getTreeSum () {
return Forest.this .sum_tree;
}
public int getTreeType () {
return Forest.this .getTreeType();
}
}
}
public class ForestClient {
public static void main (String[] args) {
Forest.MagicalAnimal man=new Forest().new MagicalAnimal();
man.setName("Monkey" );
System.out .println(man.getTreeSum());
System.out .println(man.getTreeType());
}
}
(2)静态内部类
静态内部类和普通内部类,从形式上看区别不大,但是访问内容的差别是比较大的。
①在内部类定义的前面有static限定,例如:static class A
②在静态内部类中,也可以有实例变量和实例方法;但是在普通内部类中,不能有静态变量和静态方法。
③在静态内部类中,访问外部类的静态变量和静态方法,和其他类中访问外部类的静态变量和静态方法没有区别
public class Forest {
private int sum_tree=1000 ;
private int type_tree=100 ;
public int getTreeType () {
return type_tree;
}
private static int staticValue=10000 ;
public static int getTreeSum () {
return 1000 ;
}
class MagicalAnimal{
private String name;
public void setName (String name) {
this .name=name;
}
public int getTreeSum () {
return Forest.this .sum_tree;
}
public int getTreeType () {
return Forest.this .getTreeType();
}
}
static class A{
private int i=1 ;
public int getI () {
return i;
}
public int getStaticValue () {
return Forest.staticValue;
}
public int getTreeSum () {
return Forest.getTreeSum();
}
}
}
public static void main (String[] args) {
Forest.A a=new Forest.A();
System.out .println(a.getI());
System.out .println(Forest.A.getValue());
}