马士兵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


 

 

 


 

 

目录
相关文章
【yolo训练数据集】标注好的垃圾分类数据集共享
【yolo训练数据集】标注好的垃圾分类数据集共享
4767 265
【yolo训练数据集】标注好的垃圾分类数据集共享
|
10月前
|
人工智能 运维 Cloud Native
云原生 Meetup,AI 应用工程化专场·广州站
欢迎莅临广州市海珠区鼎新路 88 号广州阿里中心,O-N-10-02 春秋书院。报名成功后,您将在活动前一周收到短信通知。
172 90
|
5月前
|
机器学习/深度学习 人工智能 算法
人机融合智能 | “人智交互”跨学科新领域
本文围绕“以人为中心AI(HCAI)”理念,提出人-人工智能交互(人智交互)这一跨学科领域及框架。文章定义了人智交互的基本理论、关键问题与方法,并探讨其开发流程和团队协作模式,强调该领域的研究意义。文中分析了智能时代人机交互的新特征,提出“人智组队”的新型人机关系,指出智能系统可作为“辅助工具+合作队友”存在。同时,文章通过对比AI学科与人因科学的优势与不足,阐明跨学科合作的必要性,为未来人智交互研究提供方向。本章旨在为后续内容构建基于HCAI理念的研究与应用框架。
511 0
|
运维
老司机,思路就是清晰!Eth-Trunk无法转发流量竟然是这样定位的!
老司机,思路就是清晰!Eth-Trunk无法转发流量竟然是这样定位的!
215 0
|
算法 搜索推荐 Java
太实用了!阿里内部强推的超全Java算法学习指南,已被彻底征服
算法和数据结构一直以来都是程序员的基本内功。 数据结构可以看作是算法实现的容器,通过一系列特殊结构的数据集合,能够将算法更为高效而可靠地执行起来。
|
图形学
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版4(附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版4(附带项目源码)
286 0
|
JavaScript API
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts
|
域名解析 SQL 监控
Web Web Application Firewall
阿里云Web应用防火墙(Web Application Firewall,简称WAF)是一种网络安全服务,用于保护Web应用程序免受常见的Web攻击,如SQL注入、跨站脚本(XSS)和跨站请求伪造(CSRF)等。它可以通过配置规则和策略,识别和拦截恶意流量,从而保护Web应用程序的安全。
406 1
|
关系型数据库 MySQL
mysql下载源码方法
方法一 进入mysql官网:http://dev.mysql.com/downloads/mysql/ 选择相关的平台下载:     3.选择Source Code 选型后,拉倒网页下方,选择要下载的源码包         4.
14320 2
|
存储 缓存 索引
数据结构——顺序表的概念和基本操作(超全超详细)
数据结构——顺序表的概念和基本操作(超全超详细)