马士兵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训练数据集】标注好的垃圾分类数据集共享
4411 250
【yolo训练数据集】标注好的垃圾分类数据集共享
|
3月前
|
JavaScript 前端开发 Java
垃圾分类管理系统基于 Spring Boot Vue 3 微服务架构实操指南
本文介绍了基于Java技术的垃圾分类管理系统开发方案与实施案例。系统采用前后端分离架构,后端使用Spring Boot框架搭配MySQL数据库,前端可选择Vue.js或Java Swing实现。核心功能模块包括垃圾分类查询、科普教育、回收预约等。文中提供了两个典型应用案例:彭湖花园小区使用的Swing桌面系统和基于Spring Boot+Vue的城市管理系统,分别满足不同场景需求。最新技术方案升级为微服务架构,整合Spring Cloud、Redis、Elasticsearch等技术,并采用Docker容器
172 0
|
3月前
|
机器学习/深度学习 人工智能 算法
人机融合智能 | “人智交互”跨学科新领域
本文围绕“以人为中心AI(HCAI)”理念,提出人-人工智能交互(人智交互)这一跨学科领域及框架。文章定义了人智交互的基本理论、关键问题与方法,并探讨其开发流程和团队协作模式,强调该领域的研究意义。文中分析了智能时代人机交互的新特征,提出“人智组队”的新型人机关系,指出智能系统可作为“辅助工具+合作队友”存在。同时,文章通过对比AI学科与人因科学的优势与不足,阐明跨学科合作的必要性,为未来人智交互研究提供方向。本章旨在为后续内容构建基于HCAI理念的研究与应用框架。
184 0
|
8月前
|
人工智能 运维 Cloud Native
云原生 Meetup,AI 应用工程化专场·广州站
欢迎莅临广州市海珠区鼎新路 88 号广州阿里中心,O-N-10-02 春秋书院。报名成功后,您将在活动前一周收到短信通知。
143 92
|
机器学习/深度学习 人工智能 搜索推荐
《百炼成金-大金融模型新篇章》––09.金融级AI原生的发展
百炼必定成金,新质生产力会催生新质劳动力,谨以此文抛砖引玉,希望与业内的各位朋友一同探讨如何积极拥抱并运用大模型技术,以应对和驾驭不断变化的市场环境,实现科技金融持续稳定的提质增效和创新发展,携手开启金融大模型未来新篇章。
235 3
|
机器学习/深度学习 数据挖掘
西浦、利物浦大学提出:点云数据增强首个全面综述
【5月更文挑战第26天】西交利物浦大学和利物浦大学的研究团队发表了一篇关于点云数据增强的首部全面综述,分析了点云增强技术在缓解深度学习模型过拟合问题上的作用。研究将方法分为基本(如仿射变换、随机丢弃)和高级(混合、对抗性变形)两类,并探讨了各类方法的优缺点及应用场景。尽管基本方法常用,但自动优化组合和参数、多模态增强及性能评估标准仍是挑战。该综述为研究者提供了理解与应用点云增强的指导,但也指出在某些领域的深入探讨尚不足。[arXiv:2308.12113]
411 1
|
图形学
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版4(附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版4(附带项目源码)
218 0
|
算法 搜索推荐 Java
太实用了!阿里内部强推的超全Java算法学习指南,已被彻底征服
算法和数据结构一直以来都是程序员的基本内功。 数据结构可以看作是算法实现的容器,通过一系列特殊结构的数据集合,能够将算法更为高效而可靠地执行起来。
|
域名解析 SQL 监控
Web Web Application Firewall
阿里云Web应用防火墙(Web Application Firewall,简称WAF)是一种网络安全服务,用于保护Web应用程序免受常见的Web攻击,如SQL注入、跨站脚本(XSS)和跨站请求伪造(CSRF)等。它可以通过配置规则和策略,识别和拦截恶意流量,从而保护Web应用程序的安全。
356 1
|
JavaScript API
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts
Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts

热门文章

最新文章