重学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 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- HandlerMethodArgumentResolver 参数解析器
关于springMvc中的参数解析器 springMvc中的HandlerAdapter会检测所有的 HandlerMethodArgumentResolver(对参数的解析器) HandlerMet ...
- Oracle -- 连接每行的内容
select wm_concat(message) from ( select rownum no, 'Case ''' || code || '''' || '' || chr(10) ...
- WCP源码分析 与SpringMVC学习资料
1.在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便. Spring2.5为我们引入了组件自动扫描机制,他可以在 ...
- Circling Round Treasures CodeForces - 375C
C. Circling Round Treasures time limit per test 1 second memory limit per test 256 megabytes input s ...
- EasyPlayerPro windows播放器本地音频播放音量控制实现
背景描述 作为一个播放器, 除了能播放视频和声音外,音量控制是绝对不能缺少的功能; 本文在音视频播放的基础上,增加对音量的控制: 实现流程 调用mixerGetDevCaps获取音频输出设备列表; 打 ...
- C#获取网页内容的三种方式(转)
搜索网络,发现C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse... 方法一:使用WebClient (引用 ...
- mysql insert返回主键
使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...
- JavaEE与Spring
在Java社区中,Spring与Java EE之争是个永恒的话题.在这场争论中,来自两个阵营的布道师.架构师与铁杆粉丝都在不遗余力地捍卫着本方的尊严,并试图说服对方加入到自己的阵营当中,但结果却是双方 ...
- 一起来学linux:进程
简单来说,每当执行一个程序或者命令,启动事件的时候都会得到一个PID,也就是进程ID.比如用户登陆的时候就会得到一个PID.如下所示.两个用户zhf和root在登陆后分别获得PID 3212和3214 ...
- (转)扫盲--JavaScript的立即执行函数
看过jQuery源码的人应该知道,jQuery开篇用的就是立即执行函数.立即执行函数常用于第三方库,好处在于隔离作用域,任何一个第三方库都会存在大量的变量和函数,为了避免变量污染(命名冲突),开发者们 ...