ITeye博客

对关键字final、static的理解

xiaozhouzhou · 2013-05-10 23:36 · java语言 · 编程语言 · Markdown

一、final关键字 final在java中表示的意思是最终,也可以称为完结器。可以使用final关键字声明类属性方法,在声明是需注意以下几点: 1)使用final声明的类不能有子类; 2)使用final声明的方法不能被子类所覆写; 3)使用final声明的变量即成为常量,常量不可以修改。 使用final声明的类不能有子类

//使用final定义类,不能被继承
final class Student{
}
//错误,不能继承使用final声明的类
class Unstudent extends Student{
}

使用final修饰的方法不能被子类覆写

class Student{
      //使用final定义的方法不能被覆写
      public final void print(){
          System.out.print("执行了");
      }
}
class Unstudent extends Student{
      //错误,不能覆写用final声明的方法
      public final void print(){
           System.out.print("成功了"); 
      }
}

被final修饰的变量即成为常量,常量不可以修改

calss Student{
     //使用final声明的变量就是常量
     private final String name="张三";
     public void print(){
          //错误,常量不能被修改
          name="李斯"
     }
}

二、static关键字 static是为静态的意思,也可称为全局。如果现在有些属性希望杯所有的对象共享,则就必须将其声明为static属性,而且一个属性使用了static关键字声明之后,可以直接使用类名称进行调用。如果一个鳄梨中的方法想由类调用,则可以声明为static方法。 1)用static声明的属性成为全局属性(也叫静态属性); 2)用static声明的方法有事被称为“类方法”,可以有类名称直接调用,且静态方法中用到的属性必须都是静态属性;

使用static声明属性

class Person {
	String name;
	int age;
        //用static修饰属性变量,则为常量
	static String city="BJ";
        //构造器传参
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("姓名:"+this.name+",年龄:"+this.age+",城市:"+city);
	}
};
public class People{
	public static void main(String args[]){
                //实例化对象
		Person p1=new Person("张三",25);
		Person p2=new Person("李斯",30);
		Person p3=new Person("王五",35);
		System.out.println("***********修改前*********");
		p1.info();
		p2.info();
		p3.info();
		System.out.println("***********修改后*********");
                //重新给属性city赋值
		p1.city="SH";
		p1.info();
		p2.info();
		p3.info();
	}
}

运行结果为: 下面同过一组图解来说明static声明的属性: a)属性city未改变前 b) 属性city未改变后
以上程序已经基本完成了修改功能,如我们所知一个类中的公共属性现在由一个对象修改,这样是不合适的。类的公共属性应该由类进行修改是最合适的,因为我们不知道一个类中到底有多少个对象产生。所以在访问static属性时最好可以由类名直接调用,那么有事也就把使用static声明的属性称为类属性。 类属性调用的格式:类名称.static 属性名;上面的代码就可以改为:Person.city="SH"

使用static声明方法

class Person {
       //对所有的属性进行封装
	 private String name;
	 private int age;
	 private static String city="BJ";
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("姓名:"+this.name+",年龄:"+this.age+",城市:"+city);
	}
      //使用static声明方法
	public static void setcity(String c) {
		city=c;
	}
	public static String getCity(){
		return city;
	}
};
public class People{
	public static void main(String args[]){
		Person p1=new Person("张三",25);
		Person p2=new Person("李斯",30);
		Person p3=new Person("王五",35);
		System.out.println("***********修改前*********");
		p1.info();
		p2.info();
		p3.info();
		System.out.println("***********修改后*********");
              //直接使用类名称调用
		Person.setcity("SH");
		p1.info();
		p2.info();
		p3.info();
	}
}

运行结果为: 在以上程序中,Person类将所有的属性都进行封装,所以想要设置属性就必须使用setter()方法,但是这里的方法是使用static声明的,所以可以直接使用类名称调用。 特别要提的是,飞static生命的方法可以调用static声明的属性或方法。但是static声明的方法是不能调用非static类型声明的属性或方法。 使用static声明的方法调用非static方法和属性(错误代码)

class Person{
     private static string city="BJ";
     private String name="张三";
     public static void sPrint(){
          //打印属性name(此处错误,不能调用非static属性)
          System.out.println("name="+name);
         //调用方法print(此处错误,不能调用非static方法)
          print();
     }
     public void print(){
          System.out.println("成功了");
     }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。