重学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 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- 搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
6月12日,搜狐新闻APP最新版本在华为应用市场正式上线啦! 那么,这一版本的搜狐新闻APP有什么亮点呢? 先抛个图,来直接感受下—— 模糊图片,瞬间清晰! 效果杠杠的吧. 而藏在这项神操作背后的 ...
- 我眼中的Oracle Database Software 和 Oracle Database
我眼中的Oracle Database Software 和 Oracle Database 我喜欢用微软的office软件和word文档(确切的说是:自己写的word文档,能够把这个Word文档想象 ...
- Microsoft-office 常见问题
1.工作表写入保护,忘记密码,解决办法: 流程如下: 1打开文件2工具---宏----录制新宏---输入名字如:aa3停止录制(这样得到一个空宏)4工具---宏----宏,选aa,点编辑按钮5删除窗口 ...
- JavaScript之this的工作原理
JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 1.全局范围内 当在全部范围内使用 this,它将会指向全局对象. 2.函 ...
- 知识复习(LDT+TSS+GATE+INTERRUPT)
[1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...
- 微信小程序TabBar的使用
一.TabBar使用步骤 1.创建所需要的界面和所需要的图片: 2.配置文件: 我们找到项目根目录中的配置文件 app.json 加入如下配置信息 "tabBar": { &quo ...
- python 基础 2.6 break用法
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- Jquery和JS的区别
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- WCP源码分析 与SpringMVC学习资料
1.在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便. Spring2.5为我们引入了组件自动扫描机制,他可以在 ...
- synchronized同步关键字
参考:http://blog.csdn.net/luoweifu/article/details/46613015 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: ...