重学JAVA基础(五):面向对象
1.封装
import java.util.Date;
public class Human {
protected String name;
protected BirthDay birthDay;
protected String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public BirthDay getBirthDay() {
return birthDay;
}
public void setBirthDay(BirthDay birthDay) {
this.birthDay = birthDay;
}
public int getAge(){//封装方法
if(birthDay!=null){
Date nowDate = new Date(System.currentTimeMillis());
int year = nowDate.getYear()+1900;
int month = nowDate.getMonth()+1;
int day = nowDate.getDate();
int age = year - this.birthDay.getYear();
if(this.birthDay.getMonth()>month){
return age-1;
}else if(this.birthDay.getMonth()==month){
if(this.birthDay.getDay()>=day){
return age-1;
}
}
return age;
}
return 0;
}
public String toString(){
return "name:"+name+" sex:"+sex+" age:"+getAge();
}
public String toString(Human human){
return this+"";
}
public Human(){
}
public Human(String name,String sex) {
super();
this.name = name;
this.sex = sex;
}
public Human(String sex) {
super();
this.sex = sex;
}
public Human(String name,String sex,int year,int month,int day) {
super();
this.name = name;
this.birthDay = new BirthDay(year, month, day);
this.sex = sex;
}
}
public class BirthDay {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public BirthDay(){
}
public BirthDay(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
}
2.继承
public class Woman extends Human{
public Woman(){
super("Woman");
}
public Woman(String name){
super(name,"Woman");
}
public String toString(){
return " I am Woman,"+super.toString();
}
public String toString(Woman woman){
return woman+"";
}
}
public class Man extends Human{
public Man(){
super("Man");
}
public Man(String name){
super(name,"Man");
}
public String toString(){
return " I am Man,"+super.toString();
}
public String toString(Man man){
return man+"";
}
}
3.多态
public class TestOO {
public static void main(String[] args) {
Human man = new Man("man");
Human woman = new Woman("woman");
System.out.println(woman.toString(man));
System.out.println(man.toString(woman));
}
}
运行结果:
I am Woman,name:woman sex:Woman age:0
I am Man,name:man sex:Man age:0
重学JAVA基础(五):面向对象的更多相关文章
- 重学JAVA基础(八):锁的基本知识
1.线程状态 如上图,当我们新建一个线程,并start后,其实不一定会马上执行,因为只有操作系统调度了我们的线程,才能真正进行执行,而操作系统也随时可以运行其他线程,这时线程又回到可运行状态.这个过程 ...
- 重学JAVA基础(二):Java反射
看一下百度的解释: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息 ...
- 重学JAVA基础(一):PATH和CLASSPATH
我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar 都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...
- 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep
/** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...
- 重学JAVA基础(六):多线程的同步
1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...
- 重学JAVA基础(四):线程的创建与执行
1.继承Thread public class TestThread extends Thread{ public void run(){ System.out.println(Thread.curr ...
- 重学JAVA基础(三):动态代理
1.接口 public interface Hello { public void sayHello(); } 2.实例类 public class Hello2 { public void sayH ...
- 12天,这本《重学Java设计模式》PDF书籍下载量9k,新增粉丝1400人,Github上全球推荐榜!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言
- 重学 Java 设计模式:实战抽象工厂模式
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- 四、Silverlight中使用MVVM(四)——演练
本来打算用MVVM实现CRUD操作的,这方面例子网上资源还挺多的,毕竟CRUD算是基本功了,因为最近已经开始学习Cailburn框架了,感觉时间 挺紧的,这篇就实现其中的更新操作吧. 功能很明确,当我 ...
- HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程
在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...
- EF6&EFCore 注册/使用实体类的正确姿势
首先回顾下EF中常规使用流程 1.新建实体类以及实体配置(data annotation或fluent api) [Table("Users")] public class Use ...
- l两张图片轮播
在head里面加 <script language="javascript"> function scroll(spanlevel) { if (spanlevel.s ...
- NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明
该篇博文主要用来说明EasyNVR硬件录像回放版本的相关接口说明和调用的demo: 方便用户的二次开发和集成. 软件根目录会包含接口文档的,因此,本文主要是对一些特定接口的说明和接口实现功能的讲解以及 ...
- ElasticSearch架构思考(转)
一个ElasticSearch集群需要多少个节点很难用一种明确的方式回答,但是,我们可以将问题细化成一下几个,以便帮助我们更好的了解,如何去设计ElasticSearch节点的数目: 打算处理多少数据 ...
- My97datepicker日期控件
转自:http://www.my97.net/dp/demo/index.htm 非常不错的一篇文章,介绍的很详细.感兴趣的朋友可以好好研究一下. 网上资源很多的,可以下一个来使用. 先说一下整个文件 ...
- 九度OJ 1061:成绩排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17158 解决:4798 题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相 ...
- Git you are not allowed to push code to protected branches on this project?
error: You are not allowed to push code to protected branches on this project....error: failed to pu ...
- VC调用Delphi DLL
别的没什么,是一定可以调用成功的.但是意外的是,ShowMessage函数在DLL里也可以轻易被调用.此外,Delphi里的var 相当于VC里的引用,需要在函数原型里正确标识,否则传递普通变量甚至常 ...