- 浏览: 832328 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (379)
- struts (5)
- hibernate (16)
- spring (16)
- ssh (20)
- MySQL (16)
- 数据库脚本 (2)
- DownLoad (1)
- GAE (5)
- Java (103)
- LoadRunner (2)
- VF (1)
- 学习资料 (24)
- 软件使用 (21)
- 通信类 (4)
- 生活 (3)
- J2ME (1)
- 心理学 (1)
- Linux (26)
- Android (3)
- Oracle (1)
- 面向对象概念&面试准备 (11)
- ExtJs (2)
- Google Map (1)
- Flex (47)
- 算法研究 (1)
- share (20)
- python (1)
- MongoDB (7)
- centos6 (13)
- C++ (8)
- DB2 (3)
- C# (1)
- 代码片段 (24)
- Lucene (2)
- php (1)
- NodeJS (1)
- Express (1)
最新评论
-
shua1991:
已阅,我表示同意。
Eclipse统计代码行数 -
nakedou:
写的不错,挺详细的
在CentOS中使用 yum 安装MongoDB及服务器端配置 -
sjp524617477:
好方法
Eclipse统计代码行数 -
simpletrc:
<script>ale ...
Java写到.txt文件,如何实现换行 -
csdn_zuoqiang:
Apache Ftp Server,目前是1.0.4,非常好的 ...
Apache FtpServer在64位系统下服务不能启动解决方法
import java.util.*; public class Employee { //构造器,-------------------------------------------------注意参数的命名规则 //默认构造器 public Employee() { } public Employee(String firstName , String lastName , double salary , int year, int month, int day) { this.firstName = firstName ;//不要在构造器中定义与实例域重名的局部变量,如:String firstName = f ; this.lastName = lastName ; this.salary = salary ; GregorianCalendar calendar = new GregorianCalendar(year,month - 1,day); // GregorianCalendar uses 0 for January this.hireDay = calendar.getTime() ; } public Employee(String firstName , String lastName , double salary) { this.firstName = firstName ; this.lastName = lastName ; this.salary = salary ; } public Employee(double salary) { this.salary = salary ; } /*public Employee(double salary) { //calls Employee(String, String, double) this() ; //nextId++ ; }*/ // 定义方法 public int getId() { return id ; } public String getName() { return firstName+" "+lastName ; } public double getSalary() { return salary ; } //----------------------------------------------若需返回一个可变数据域的拷贝,应该使用克隆,以免破坏封装性 public Date getHireDay() { return (Date)hireDay.clone() ; } //----------------------------------------------关键字this表示隐式参数 public void raiseSalary(double byPercent) { double raise = this.salary * byPercent /100 ; this.salary += raise ; } // 定义 equals 方法======================================================================= public boolean equals(Object otherObject) { // a quick test to see if the objects are identical if(this == otherObject) return true ; // must return false if the explicit parameter is null if(otherObject == null) return false ; //if the classes don't match , they can't be equal if(getClass() != otherObject.getClass()) return false ; // now we know otherObject is a non-null Employee Employee other = (Employee) otherObject ; // test whether the fields have idential values return firstName.equals(other.firstName) && lastName.equals(other.lastName) && salary == other.salary && hireDay.equals(other.hireDay) ; } // 定义hashCode方法====================================================================== public int hashCode() { return 7 * firstName.hashCode() + 11 * lastName.hashCode() + 13 * new Double(salary).hashCode() + 17 * hireDay.hashCode() ; } // 定义 toString 方法======================================================================= public String toString() { return getClass().getName() + "[ firstName= " + firstName + " , lastName= " + lastName + " , salary= " + salary + " , hireDay= " + hireDay +" ] " ; } //定义 域 private static int nextId ; private int id ; private String firstName = " ";//实例域初始化 private String lastName = " ";//实例域初始化 /* private final String firstName ;//String类是一个不可变的类,声明为final类型 private final String lastName ;//表示对象在构建之后不会再被修饰,即没有setName方法 */ private double salary ; private Date hireDay ; //静态初始化块 static { Random generator = new Random() ; //set nextId to a random number between 0 and 9999 nextId = generator.nextInt(10000) ; } //对象初始化块 { id = nextId ; nextId++ ; } }
public class Manager extends Employee { // 定义构造器,通过super 调用超类的构造器 public Manager(String firstName , String lastName , double salary , int year , int month , int day ) { super(firstName , lastName , salary , year , month , day) ; bonus = 0 ; } // 定义子类的getSalary方法,覆盖超类中的getSalary方法 public double getSalary() { double baseSalary = super.getSalary() ;//通过super调用超类的getSalary方法 return baseSalary + bonus ; } public void setBonus(double bonus) { this.bonus = bonus ; } // 定义子类Manager对象的equals方法,通过super调用超类的equals方法 public boolean equals(Object otherObject) { if(!super.equals(otherObject)) return false ; Manager other = (Manager) otherObject ; // super.equals checked that this and other belong to the same class return this.bonus == other.bonus ; } // 定义子类的hashCode方法 public int hashCode() { return super.hashCode() + 19 * new Double(bonus).hashCode() ; } // 定义子类的toString方法 public String toString() { return super.toString() + "[ bonus= " + bonus + " ]" ; } private double bonus ; }
public class EqualsTest { public static void main(String[] args) { Employee alices1 = new Employee("Alice", "Adams", 75000, 1987, 12, 15) ; Employee alices2 = alices1 ; Employee alices3 = new Employee("Alice", "Adams", 75000, 1987, 12, 15) ; Employee bob = new Employee("Bob", "Brandson", 50000, 1989, 10, 1) ; System.out.println("alices1 == alices2: " + (alices1 == alices2)) ; System.out.println("alices1 == alices3: " + (alices1 == alices3)) ; System.out.println("alices1.equals(alices3): " + alices1.equals(alices3)) ; System.out.println("alices1.equals(bob): " + alices1.equals(bob)) ; System.out.println("bob.toString(): " + bob) ; Manager carl = new Manager("Carl", "Cracker", 80000, 1987, 12, 15) ; Manager boss = new Manager("Carl", "Cracker", 80000, 1987, 12, 15) ; boss.setBonus(5000) ; System.out.println("boss.toString(): " + boss) ; System.out.println("carl.equals(boss): " + carl.equals(boss)) ; System.out.println("alices1.hashCode(): " + alices1.hashCode()) ; System.out.println("alices3.hashCode(): " + alices3.hashCode()) ; System.out.println("bob.hashCode(): " + bob.hashCode()) ; System.out.println("carl.hashCode(): " + carl.hashCode()) ; } }
public class EmployeeTest { public static void main(String[] args) { //fill the staff array with three Employee objects Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl","Crack",75000,1987,12,15); staff[1] = new Employee("Harry","Hacker",50000,1989,10,1); staff[2] = new Employee("Tony","Tester",40000,1990,3,15); //raise everyone's salary by 5% for(Employee e : staff) e.raiseSalary(5);//使用for each 循环结构 //print out information about all Employee objects for(Employee e : staff) System.out.println("name= " + e.getName() +",salary= "+e.getSalary() +",hireDay= "+e.getHireDay()); } }
public class ManagerTest { public static void main(String[] args) { // constructor a Manager object Manager boss = new Manager("Carl" , "Cracker" , 80000 , 1987 , 12 ,15) ; boss.setBonus(5000) ; Employee[] staff = new Employee[3] ; // fill the staff array with Manager and Employee objects staff[0] = boss ; //置换法则,将Manager类对象赋值给Employee类对象 staff[1] = new Employee("Harry" , "Hacker" , 50000 , 1989 , 10 ,1) ; staff[2] = new Employee("Tommy" , "Tester" , 40000 , 1990 , 3 , 15) ; //print out information about all Employee objects for(Employee e : staff) System.out.println("name=" +e.getName() + ", salary= "+e.getSalary()) ;//此处的变量e即引用了超类Employee的getSalary方法,又同时引用了子类Manager的getSalary方法,这种现象叫做多态。 } }
public class ParamTest { public static void main(String[] args) { /* Test 1: Methods can't modify numeric parameters 一个方法不能修改一个基本数据类型的参数(即数值型和布尔型值) */ System.out.println("Testing tripleValue:"); double percent = 10 ; System.out.println("Before: percent= "+percent); tripleValue(percent); System.out.println("After: percent= "+percent); /* Test 2: Methods can change the state of object parameters 一个方法可以改变一个对象参数的状态 */ System.out.println("\nTesting tripleSalary: "); Employee harry = new Employee("Harry " , "Crack" , 5000); System.out.println("Before: salary= "+harry.getSalary()); tripleSalary(harry); System.out.println("After: salary= "+harry.getSalary()); /* Test 3: Methods can't attach new objects to object parameters 一个方法不能让对象参数引用一个新的对象 */ System.out.println("\nTesting swap: "); Employee a = new Employee("Eric" , "Zhang" , 70000); Employee b = new Employee("Huiyi" , "Chang" , 50000); System.out.println("Before: a= "+a.getName()); System.out.println("Before: b= "+b.getName()); swap(a,b); System.out.println("After: a= "+a.getName()); System.out.println("After: b= "+b.getName()); } //Test 1 public static void tripleValue(double x) //does'nt work { x = 3 * x ; System.out.println("End of Method: x= "+x ); } //Test 2 public static void tripleSalary(Employee x) //works { x.raiseSalary(200); System.out.println("End of Methods: salary= "+x.getSalary()); } //Test 3 public static void swap(Employee x , Employee y) { Employee temp = x ; x = y ; y = temp ; System.out.println("End of Methods: x = "+x.getName()); System.out.println("End of Methods: y = "+y.getName()); } } /*结果如下: Testing tripleValue:--------------------------------------未发生改变 Before: percent= 10.0 End of Method: x= 30.0 After: percent= 10.0 Testing tripleSalary: ------------------------------------发生改变 Before: salary= 5000.0 End of Methods: salary= 15000.0 After: salary= 15000.0 Testing swap: --------------------------------------------a,b未发生改变,x,y发生改变 Before: a= Eric Zhang Before: b= Huiyi Chang End of Methods: x = Huiyi Chang End of Methods: y = Eric Zhang After: a= Eric Zhang After: b= Huiyi Chang */
import java.util.*; public class CalendarTest { public static void main(String[] args) { //construct d as current date构造一个日历对象 GregorianCalendar d = new GregorianCalendar(); //2次调用get方法得到当时的日、月 int today = d.get(Calendar.DAY_OF_MONTH); int month = d.get(Calendar.MONTH); // set d to start date of the month将d设置为这个月的第一天 d.set(Calendar.DAY_OF_MONTH,1); //并且获得第一个月的第一天是星期几 int weekday = d.get(Calendar.DAY_OF_WEEK); //print heading System.out.println(" Sun Mon Tue Wed Thu Fri Sat "); // indent first line of calendar for(int i = Calendar.SUNDAY; i < weekday; i++) System.out.print(" "); do { //print day int day = d.get(Calendar.DAY_OF_MONTH); System.out.printf("%4d", day); //mark current day with * if(day == today) System.out.print("*"); else System.out.print(" "); //start a new line after every Saturday if(weekday == Calendar.SATURDAY) System.out.println(); //advanced d to the next day d.add(Calendar.DAY_OF_MONTH,1); weekday = d.get(Calendar.DAY_OF_WEEK); } while (d.get(Calendar.MONTH) == month); // the loop exits when d is day 1 of the the next month //print final end of line if necessary if (weekday != Calendar.SUNDAY) System.out.println(); } }
评论
1 楼
csdn_zuoqiang
2010-07-18
100以内的质数(素数):2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 (共25个)、
在计算hashCode的时候用得着
在计算hashCode的时候用得着
发表评论
-
微信JS
2013-10-26 21:17 2046<div class="iteye-blog- ... -
ubuntu下MySQL用source命令导入sql文件出现乱码解决方法
2012-11-18 23:46 1492首先建立数据库的时候指明数据库编码如: CREA ... -
RandomAccessFile
2012-10-18 18:16 947public void run() { try { ... -
java中多种方式读文件
2012-10-18 16:53 933java中多种方式读文件一、多种方式读文件内容。1、按字节读取 ... -
FileChannelMain
2012-10-15 18:12 1079package scan; import java ... -
Apache FtpServer在64位系统下服务不能启动解决方法
2012-06-10 21:29 6809Apache FTPServer是一款用Java开发的 ... -
Java 集合类
2012-06-07 22:03 1587Java 集合类 1. 为什么要了解J ... -
short、int、long与byte之间的转换工具类
2012-05-31 11:05 4480/** * 各基础类型与byte之间的转换 * ... -
Linux Mint 13 配置JAVA 环境
2012-05-24 22:35 26230.1--下载 JAVA ... -
FatJar+Exe4j+Inno Setup 生成可执行的exe文件
2012-04-17 10:54 14321、fatjar 是Eclipse的一个免费的插件。它的 ... -
JPanel JTextField add Focus 获取焦点解决方案
2012-03-30 21:29 2982public class TabPagePanel ex ... -
JList List<E> Page 分页
2012-03-30 21:28 1735package view.retrieve.comps. ... -
JButton setAction的BUG
2012-03-23 10:53 1278今天在使用JButton的时候,想用setText()setI ... -
自定义JTabbedPane皮肤
2012-03-22 12:05 4715package ui; import java.awt. ... -
两个工具类
2012-03-17 21:27 870package com.retrieve.utils; ... -
两个工具类
2012-03-17 21:27 0package com.retrieve.utils; ... -
mysql、sqlserver、oracle分页,java分页统一接口实现
2012-03-13 17:56 0定义: pageStart 起始页,pageEnd 终止页, ... -
Invalid command: InetLoad::load
2012-03-06 16:41 1291Invalid command: InetLoad::load ... -
NIO: High Performance File Copying
2012-03-01 17:25 1160In a previous tip, I discussed ... -
自定义JList
2012-02-03 15:39 779自定义JList。
相关推荐
要想学习java corejava是java之旅的最重要的入门知识,本人在学习corejava中练习过的最重量级的代码! 希望对新手有所帮助!
java 核心技术 学习资料 分两卷:1 基础知识 2.高级特性
gst1-java-core是一组Java绑定。 GStreamer是一个用C语言编写的开源,基于管道的多媒体框架。它允许程序员在应用程序内部创建各种媒体处理管道,从简单的媒体播放到编码,实时流式传输,分析,机器学习, WebRTC等...
core java源码 Java Core 、Java 源码 、JVM 方面的学习 java-core java 集合 IO NIO 网络 线程 方面学习 java-source 集合部分 线程 等 源码学习 java-jvm java 虚拟机方面学习 测试
学习spring不可少的包,commons-logging.java,hamcrest-core-1.3.java,junit-4.11.java,log4j-1.2.17.java,mysql-connector-java-5.1.39-bin.java,spring-aop-4.2.9.RELEASE.java,spring-beans-4.2.9.RELEASE.java,...
Spring5.2.6相关环境资源,以及JdbcTemplate学习过程中需要的jar包,解压后将需要的jar包导入项目即可使用。包括:mysql-connector-java-8.0.29.jar、com.springsource.net.sf.cglib-2.2.0.jar、...
核心Java 核心Java信息 创建于:Vishal Gajera 该存储库的创建是基于其目的,是为了帮助学生/初学者可以(从概念上)从远程学习,并减少书面工作,并且在技术上变得更加复杂。
该资源主要是corejava中一些重要的知识点,都是java的基础,把java学好,这些知识的必须得熟练掌握的,是学习后面相关java开发技术的垫脚石,这些学好了后面的就容易了。希望这写东西能帮助到你!
apache开源项目源码ibatis-3-core-src-3.0.0.227 ibatis框架java源程序 spring,struts,hibernate,ibatis,框架源码 各种ibatis框架应用源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其...
Core Java 学习必备PPT(CoreJava教案)
java\CoreJava笔记\CoreJava_day06.doc
达内core_java学习笔记,初学core_java者推荐文档
主要用于CoreJava试题,可以帮助同学们更好的学习Java的核心知识。
java8集合源码核心Java学习与实验 Java 8 特性 Java流: 参考: 要对数据源的元素执行一系列操作并聚合它们的结果,需要三个部分 - 源、中间操作和终端操作。 中间操作返回一个新的修改过的流。 如果需要多个修改,...
corejava接口学习总结.pdfcorejava接口学习总结.pdfcorejava接口学习总结.pdfcorejava接口学习总结.pdfcorejava接口学习总结.pdfcorejava接口学习总结.pdfcorejava接口学习总结.pdf
COREJAVA学习代码COREJAVA学习代码COREJAVA学习代码
JAVA 学习记录一CoreJava.rarJAVA 学习记录一CoreJava.rarJAVA 学习记录一CoreJava.rar
java开发的网上商城源代码,适合初学者学习之用
关于Java核心技术学习积累的例子,是初学者及核心技术巩固的最佳实践
学习java的书籍 core java。想学的可以看看。讲的很全面,挺不错。