zoukankan      html  css  js  c++  java
  • java中final、super、static关键字的使用

    
    // 人类
    class Person {
    	
    	public String name;
    	public String gender;
    	public int age;
    	// final关键字的使用
    	public final String school = "hbuas";
    	// static关键字的使用
    	public static String department;
    	public void say() {
    		System.out.println("Hello!");
    	}
    	
    	public void show() {
    		System.out.println("name: " + name);
    		System.out.println("gender: " + gender);
    		System.out.println("age: " + age);
    	}
    	
    }
    
    // 学生类
    class Student extends Person {
    	
    	public int grade;
    	Student(String name, String gender, int age, int grade) {
    		this.name = name;
    		this.gender = gender;
    		this.age = age;
    		this.grade = grade;
    	}
    	
     	public void study() {
    		System.out.println("good good learn!");
    	}
    	
    	public void show() {
    		
    		// super关键字的使用
    		super.show();
    		System.out.println("I am a student.");
    	}
    	
    	
    }
    
    // 教师类
    class Teacher extends Person {
    	
    	public String course;
    	
    	Teacher(String name, String gender, int age, String course) {
    		this.name = name;
    		this.gender = gender;
    		this.age = age;
    		this.course = course;
    	}
    	
    	public void teach() {
    		
    		System.out.println("good good work!");
    	}
    	
    	public void show() {
    		
    		// super关键字的使用
    		// 调用父类中的show方法
    		super.show();
    		System.out.println("I am a teacher.");
    	}
    }
    
    // 测试类
    class demo {
    	
    	public static void main (String []args) {
    		
    		Student s = new Student("Tom", "male", 20, 2);
    		// 为静态变量赋值
    		s.department = "computer";
    		System.out.println("school: " + s.school);
    		System.out.println("department: " + s.department);
    		s.show();
    		s.study();
    	
    		System.out.println("——————————————————————————————");
    		
    		Teacher t = new Teacher("Wang", "male", 20, "math");
    		System.out.println("school: " + t.school);
    		// 直接输出静态变量
    		System.out.println("department: " + t.department);
    		t.show();
    		t.teach();
    	}
    	
    }
    
    
    

  • 相关阅读:
    输入框正则表达式验证
    MySQL表名、列名区分大小写详解
    前后台交互
    分页写法
    web程序调试方法
    html 标签
    Aborting commit: 'XXXXXXXX'remains in conflict错误
    返回按钮
    跳出frameset框架
    fastadmin中关联表时A为主表,想让B表和C表关联时怎么办?
  • 原文地址:https://www.cnblogs.com/TomHe789/p/12907531.html
Copyright © 2011-2022 走看看