马士兵J2SE-第三章-面向对象-static、继承、重写、构造函数

简介:

 

static关键字 静态成员变量

  

public class Cat{
	private static int sid=0;
	private String name;
	int id;
	
	Cat(String name){
		this.name=name;
		id=sid++;
	}
	
	public void info(){
		System.out.println("My name is "+name+" No."+id);
	}
	
	public static void main(String[] args) {
		Cat tom=new Cat("tom");
		tom.sid=100;
		Cat jack=new Cat("jack");

		tom.info();
		jack.info();
	}
}


输出:

My name is tom No.0
My name is jack No.100

 

 

 使用ECLIPSE ALT+SHIFT+S自动快速生成的代码

类的继承与权限控制

class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}

class Student extends Person {
	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	
}

public class test {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("zzk");
		student.setAge(23);
		student.setSchool("MIT");
		System.out.println(student.getName());
		System.out.println(student.getAge());
		System.out.println(student.getSchool());
	}
}

zzk
23
MIT


重写:

class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
    
	public String getInfo() {
		return "Name: "+name+"\n"+"age:"+age;
	}
}


class Student extends Person {
	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String getInfo() {
		return "Name: "+getName()+"\nage"+getAge()+"\nschool:"+school;
	}
}


public class test {
	public static void main(String[] args) {
		Student student=new Student();
		Person person=new Person();
		person.setName("Tom");
		person.setAge(28);
		student.setName("Jack");
		student.setAge(23);
		student.setSchool("MIT");
		System.out.println(person.getInfo());
		System.out.println(student.getInfo());
	}
}


输出:

Name: Tom
age:28
Name: Jack
age23
school:MIT

 

构造函数与一般成员函数在继承中的区别

class A {
	protected void print(String s) {
		System.out.println(s);
	}
	
	A() {print("A()");};
	
	public void f() {print("A:f()");};
}

class test extends A {
	test() {print("B()");};
	public void f() {print("B:f()");}
    
	public static void main(String[] args) {
		test b=new test();
		b.f();
	}
}


输出:

A()
B()
B:f()


 

构造方法调用

class Person {
	private String name;
	private String location;
	
	Person(String name) {
		this.name=name;
		location="beijing";
	}
	
	Person(String name,String location) {
		this.name=name;
		this.location=location;
	}
	
	public String info() {
		return "name: "+name+" location: "+location; 
	}
}

class Student extends Person {
	private String school;
	Student(String name,String school) {
        this(name,"beijing ",school);
	}
	Student(String n,String l,String school) {
		super(n,l);
		this.school=school;
	}
	
	public String info() {
		return super.info()+" school: "+school;
	}
}

public class test {
	public static void main(String[] ars) {
		Person p1=new Person("A");
		Person p2=new Person("B","shanghai");
		Student s1=new Student("C","S1");
		Student s2=new Student("C","shanghai","S2");
		System.out.println(p1.info());
		System.out.println(p2.info());
		System.out.println(s1.info());
		System.out.println(s2.info());
		
	}
}

输出:


name: A location: beijing
name: B location: shanghai
name: C location: beijing  school: S1
name: C location: shanghai school: S2

 

 

 

//构造Teacher类,继承Person类
//增加“职称”(String)属性
//具有和Student类类似的重载构造方法
//重写Person类的info()方法,增加“职称”信息

class Person {
	private String name;
	private String location;
	
	Person(String name) {
		this.name=name;
		location="beijing";
	}
	
	Person(String name,String location) {
		this.name=name;
		this.location=location;
	}
	
	public String info() {
		return "name: "+name+" location: "+location; 
	}
}

class Teacher extends Person {
	private String profession;
	Teacher(String name,String profession) {
		this(name,"beijing",profession);
	}
	Teacher(String n,String l,String profession) {
		super(n,l);
		this.profession=profession;
	}
	public String info() {
		return super.info()+" profession: "+profession;
	}
}


class Student extends Person {
	private String school;
	Student(String name,String school) {
        this(name,"beijing ",school);
	}
	Student(String n,String l,String school) {
		super(n,l);
		this.school=school;
	}
	
	public String info() {
		return super.info()+" school: "+school;
	}
}

public class test {
	public static void main(String[] ars) {
		Person p1=new Person("A");
		Person p2=new Person("B","shanghai");
		Student s1=new Student("C","S1");
		Student s2=new Student("C","shanghai","S2");
		Teacher t1=new Teacher("D","phd");
		Teacher t2=new Teacher("D","haerbin","doc");
		System.out.println(p1.info());
		System.out.println(p2.info());
		System.out.println(s1.info());
		System.out.println(s2.info());
		System.out.println(t1.info());
		System.out.println(t2.info());
		
	}
}
输出:



name: A location: beijing
name: B location: shanghai
name: C location: beijing  school: S1
name: C location: shanghai school: S2
name: D location: beijing profession: phd
name: D location: haerbin profession: doc


 

 

 


 

 

目录
相关文章
|
2月前
|
Java PHP 数据安全/隐私保护
PHP 面向对象,构造函数,析构函数,继承,方法的重写,接口抽象类,static,final,this,parent,self的异同和作用
本文详细介绍了PHP面向对象编程的一系列核心概念和用法,包括构造函数、析构函数、继承、方法重写、访问控制、接口、抽象类、静态成员、final关键字、以及this、self、parent这三个关键字的异同和作用。通过具体示例代码,展示了如何在PHP中使用这些面向对象的特性,以及它们在实际开发中的应用。
PHP 面向对象,构造函数,析构函数,继承,方法的重写,接口抽象类,static,final,this,parent,self的异同和作用
|
3月前
|
Java
【Java基础面试四十二】、 static修饰的类能不能被继承?
这篇文章讨论了Java中static关键字修饰的类是否可以被继承,解释了静态内部类的概念、规则以及如何实例化。
|
5月前
|
Java 编译器 数据处理
JavaSE——面相对象高级一(4/4)-继承相关的注意事项:权限修饰符、单继承、Object类、方法重写、子类访问成员的特点......
JavaSE——面相对象高级一(4/4)-继承相关的注意事项:权限修饰符、单继承、Object类、方法重写、子类访问成员的特点......
57 0
|
Java
java学习第十三天笔记-面向对象249-继承中成员方法和成员变量的特点-利用方法的重写去设计继承结构
java学习第十三天笔记-面向对象249-继承中成员方法和成员变量的特点-利用方法的重写去设计继承结构
85 0
java学习第十三天笔记-面向对象249-继承中成员方法和成员变量的特点-利用方法的重写去设计继承结构
|
Java
java学习第十三天笔记-面向对象243-子类能继承父类的那些内容2成员变量被继承下来
java学习第十三天笔记-面向对象243-子类能继承父类的那些内容2成员变量被继承下来
75 0
java学习第十三天笔记-面向对象243-子类能继承父类的那些内容2成员变量被继承下来
|
Java
java学习第十三天笔记-面向对象242-子类能继承父类的那些内容1构造方法不能被继承下来
java学习第十三天笔记-面向对象242-子类能继承父类的那些内容1构造方法不能被继承下来
50 0
java学习第十三天笔记-面向对象242-子类能继承父类的那些内容1构造方法不能被继承下来
|
Java
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容3成员方法是否被继承
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容3成员方法是否被继承
61 0
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容3成员方法是否被继承
|
Java
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容4代码实现
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容4代码实现
75 0
java学习第十三天笔记-面向对象244-子类能继承父类的那些内容4代码实现
|
Java
java学习第十三天笔记-面向对象248-继承中成员方法和成员变量的特点-成员方法的访问特点验证
java学习第十三天笔记-面向对象248-继承中成员方法和成员变量的特点-成员方法的访问特点验证
57 0
java学习第十三天笔记-面向对象248-继承中成员方法和成员变量的特点-成员方法的访问特点验证
|
Java
java学习第十三天笔记-面向对象247-继承中成员方法和成员变量的特点-成员方法的访问特点
java学习第十三天笔记-面向对象247-继承中成员方法和成员变量的特点-成员方法的访问特点
93 0
java学习第十三天笔记-面向对象247-继承中成员方法和成员变量的特点-成员方法的访问特点