赞
踩
JDK :java development kit
JRE:java runtime environment
JDK包含JRE
java跨平台:因为java程序运行依赖虚拟机,虚拟机需要有对应操作系统的版本,而jre中有虚拟机。
当你想要在Linux系统下运行,则需要安装对应的虚拟机,及对应的jdk版本,而对应的jdk版本中的jre有对应的虚拟机
类名:不能以数字开头,只能有_ $ 字母 数字
类方法:前面小写,第二个单词大写。
【注意:八个比特等于一个字节】
package com.li.demo01; import java.io.*; import java.util.ArrayList; import java.util.Iterator; public class iterator { public static void main(String[] args) throws IOException, ClassNotFoundException { File file = new File("xxx.txt"); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream op = new ObjectOutputStream(fos); User user = new User(1,2); op.writeObject(user); op.flush(); op.close(); System.out.println(user); System.out.println("========================================="); FileInputStream fls = new FileInputStream(file); ObjectInputStream oi = new ObjectInputStream(fls); User user1 = (User) oi.readObject(); System.out.println(user1); } }
package com.li.demo01; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = -134246534L; private int id; transient int age; public User() { } public User(int id, int age) { this.id = id; this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", age=" + age + '}'; } }
输出结果
User{id=1, age=2}
=========================================
User{id=1, age=0}
进程已结束,退出代码 0
public class demo7 {
static int x;
public void run(){
int i;
System.out.println(i);//注意这里的i会报错,因为i为局部变量,局部变量没有赋值,是不能进行输出。
}
public static void main(String[] args) {
System.out.println(x);
}
}
1、抽象类一定是个父类?
是的,因为不断抽取而来的。
2、抽象类中是否可以不定义抽象方法。
是可以的,那这个抽象类的存在到底有什么意义呢?不让该类创建对象,方法可以直接让子类去使用
3、抽象关键字abstract不可以和哪些关键字共存?
1、private:私有的方法子类是无法继承到的,也不存在覆盖,而abstract和private一起使用修饰方法,abstract既要子类去实现这个方法,而private修饰子类根本无法得到父类这个方法。互相矛盾。
2、final,暂时不关注,后面学
3、static,暂时不关注,后面学
继承父类,当父类中方法有abstract,则继承时就需要重写。
interface 缉毒{ public abstract void 缉毒(); } //定义犬科的这个提醒的共性功能 abstract class 犬科{ public abstract void 吃饭(); public abstract void 吼叫(); } // 缉毒犬属于犬科一种,让其继承犬科,获取的犬科的特性, //由于缉毒犬具有缉毒功能,那么它只要实现缉毒接口即可,这样即保证缉毒犬具备犬科的特性,也拥有了缉毒的功能 class 缉毒犬 extends 犬科 implements 缉毒{ public void 缉毒() { } void 吃饭() { } void 吼叫() { } } class 缉毒猪 implements 缉毒{ public void 缉毒() { } }
多态体现为父类引用变量可以指向子类对象。
多态的前提是必须有子父类关系或者类实现接口关系,否则无法完成多态。
在使用多态后的父类引用变量调用方法时,会调用子类重写后的方法。
如:接口 变量 = new 实现类():
父类
public class Animal {//父类 static int cons = 1; int val = 1; final int fin = 1; public void eat() { System.out.println("animal eat"); } static void eat2() { System.out.println("animal eat2"); } }
子类
public class Cat extends Animal{ static int cons = 2; int val = 2; final int fin = 2; @Override public void eat() { System.out.println("cat eat"); } static void eat2() { System.out.println("cat eat2"); } }
测试类(调用父子类中一样的方法,则输出结果是子类的方法,要想调用父类的方法,需要在父类中加上static。
调用父子类中的成员,默认输出结果是父类的成员变量。原因【编译、运行都是跟着父类走】且通过多态调用的成员变量,无论是普通类型,静态类型,常量类型仍是父类的成员变量,因为成员变量不存在override(覆盖)问题)
public static void main(String[] args) {
Animal a = new Cat();//父类引用
System.out.println(a.val);//成员变量
System.out.println(a.cons);//静态变量
System.out.println(a.fin);//常量
a.eat();//成员方法
a.eat2();//静态方法
}
输出结果
1
1
1
cat eat
animal eat2
Person p = new Stduent();Person p = new Teacher();【Teacher 向上转型(优点:可以调用子类和父类的共有内容。缺点:不能调用子类中特有的内容。如何解决缺点:可以加强转。如:Teacher t = (Teacher)p;
)】
权限 方法名(参数列表){
}
方法的名字,必须和类的名字完全一致
构造方法不允许写返回值类型,void也不能写。
package cn.itcast.demo03; /* * this可以在构造方法之间进行调用 * this.的方式,区分局部变量和成员变量同名情况 * this在构造方法之间的调用,语法 this() */ public class Person { private String name; private int age; public Person(){ //调用了有参数的构造方法 //参数李四,20传递给了变量name,age this("李四",20);//调用this()需要让它再构造方法的第一行 } /* * 构造方法,传递String,int * 在创建对象的同时为成员变量赋值 */ public Person(String name,int age){ this.name = name; this.age = 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; } }
package cn.itcast.demo03;
public class Test {
public static void main(String[] args) {
//创建Person的对象,调用的是空参数的构造方法
//运行的结果 null 0
Person p = new Person();
System.out.println(p.getName());
System.out.println(p.getAge());
}
}
内存图
父类
子类
测试类
public zi(){
super();
}
内存图
如:
父类
/*
* 手动写一个父类Person类的构造方法,加入int类型参数
* 保存,子类就报错
*/
public class Person {
public Person(int a){
}
public Person(double d){
}
}
子类
/* * 子类构造方法的报错原因: 找不到父类的空参数构造器 * 子类中,没有手写构造,编译器添加默认的空参数 * public Student(){ * super(); * } * 编译成功,必须手动编写构造方法,请你在super中加入参数 * * 注意: 子类中所有的构造方法,无论重载多少个,第一行必须是super() * 如果父类有多个构造方法,子类任意调用一个就可以 * super()语句必须是构造方法第一行代码 */ public class Student extends Person{ public Student(){ super(0.1); } public Student(String s){ super(1); } }
案例:
父类
public class Person extends Object{
public Person(int a) {
}
}
子类
/* 构造方法第一行,写this()还是super() 不能同时存在,任选其一,保证子类的所有构造方法调用到父类的构造方法即可 小结论: 无论如何,子类的所有构造方法,直接,间接必须调用到父类构造方法 子类的构造方法,什么都不写,默认的构造方法第一行 super(); */ public class Student extends Person{ public Student(){ //调用的是自己的构造方法 //间接形式调用到了父类的构造方法 this("abc"); } public Student(String s){ super(1); } }
测试类
public class Test {
public static void main(String[] args) {
new Student();
}
}
final Zi z2 = new Zi();
z2 = new Zi();
classpath是帮助虚拟机找到jar包的位置
在命令窗口也可配置 如:set classpath=d:\method.jar
在eclipse中直接在项目点击右键创建一个folder为lib,再把要导入的jar包放进去即可。
/*
* 闰年计算
* 2000 3000
* 高级的算法: 日历设置到指定年份的3月1日,add向前偏移1天,获取天数,29闰年
*/
public static void function_1(){
Calendar c = Calendar.getInstance();
//将日历,设置到指定年的3月1日
c.set(2088, 2, 1);
//日历add方法,向前偏移1天
c.add(Calendar.DAY_OF_MONTH, -1);
//get方法获取天数
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
}
/* * JDK1.5后出现的特性,自动装箱和自动拆箱 * 自动装箱: 基本数据类型,直接变成对象 * 自动拆箱: 对象中的数据变回基本数据类型 */ public class IntegerDemo2 { public static void main(String[] args) { function_2(); } /* * 关于自动装箱和拆箱一些题目 */ public static void function_2(){ Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i==j);// false 对象地址 System.out.println(i.equals(j));// true 继承Object重写equals,比较的对象数据 System.out.println("==================="); Integer a = 500; Integer b = 500; System.out.println(a==b);//false System.out.println(a.equals(b));//true System.out.println("==================="); //数据在byte范围内,JVM不会从新new对象 Integer aa = 127; // Integer aa = new Integer(127) Integer bb = 127; // Integer bb = aa; System.out.println(aa==bb); //true System.out.println(aa.equals(bb));//true } //自动装箱和拆箱弊端,可能出现空指针异常 public static void function_1(){ Integer in =null; //in = null.intValue()+1 in = in + 1; System.out.println(in); } //自动装箱,拆箱的 好处: 基本类型和引用类直接运算 public static void function(){ //引用类型 , 引用变量一定指向对象 //自动装箱, 基本数据类型1, 直接变成了对象 Integer in = 1; // Integer in = new Integer(1) //in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型 //in+1 ==> in.inValue()+1 = 2 // in = 2 自动装箱 in = in + 1; System.out.println(in); } }
public static void function(){
Collection coll = new ArrayList();
coll.add("abc");
coll.add("rtyg");
coll.add("43rt5yhju");
//注意要想获取String的长度,必须先向下转型
Iterator it = coll.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s.length());
}
}
//该转型存在安全隐患,如:当我们coll.add(1);增加整数时,由于jdk中的自动装箱特性,就会把整数转为Integer。此时下面的转型会报异常,因为Integer不能转为String。
//所以,我们在写集合的时候,最好将泛型加上去,也就是在集合后面指定该存储的类型。
/* * 将的酒店员工,厨师,服务员,经理,分别存储到3个集合中 * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法 */ import java.util.ArrayList; import java.util.Iterator; public class GenericTest { public static void main(String[] args) { //创建3个集合对象 ArrayList<ChuShi> cs = new ArrayList<ChuShi>(); ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>(); ArrayList<JingLi> jl = new ArrayList<JingLi>(); //每个集合存储自己的元素 cs.add(new ChuShi("张三", "后厨001")); cs.add(new ChuShi("李四", "后厨002")); fwy.add(new FuWuYuan("翠花", "服务部001")); fwy.add(new FuWuYuan("酸菜", "服务部002")); jl.add(new JingLi("小名", "董事会001", 123456789.32)); jl.add(new JingLi("小强", "董事会002", 123456789.33)); // ArrayList<String> arrayString = new ArrayList<String>(); iterator(jl); iterator(fwy); iterator(cs); } /* * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法 work * ? 通配符,迭代器it.next()方法取出来的是Object类型,怎么调用work方法 * 强制转换: it.next()=Object o ==> Employee * 方法参数: 控制,可以传递Employee对象,也可以传递Employee的子类的对象 * 泛型的限定 本案例,父类固定Employee,但是子类可以无限? * ? extends Employee 限制的是父类, 上限限定, 可以传递Employee,传递他的子类对象 * ? super Employee 限制的是子类, 下限限定, 可以传递Employee,传递他的父类对象 * 不写继承Employee的话什么参数都能传进来,如,ArrayList<String> array = new ArrayList<String>(); */ public static void iterator(ArrayList<? extends Employee> array){ Iterator<? extends Employee> it = array.iterator(); while(it.hasNext()){ //获取出的next() 数据类型,是什么Employee Employee e = it.next(); e.work(); } } }
/* * List接口派系, 继承Collection接口 * 下面有很多实现类 * List接口特点: 有序,索引,可以重复元素 * 实现类, ArrayList, LinkedList * * List接口中的抽象方法,有一部分方法和他的父接口Collection是一样 * List接口的自己特有的方法, 带有索引的功能 */ public class ListDemo { public static void main(String[] args) { function_2(); } /* * E set(int index, E) * 修改指定索引上的元素 * 返回被修改之前的元素 */ public static void function_2(){ List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); Integer i = list.set(0, 5); System.out.println(i); System.out.println(list); } /* * List接口中的方法: * set和add有区别,虽然都可以按照索引导入数 * 据,但是set不能超过数组长度去导入数据, * 就是说set只能在原有的数组中进行导入,而 * add可以。 * */ /* * E remove(int index) * 移除指定索引上的元素 * 返回被删除之前的元素 */ public static void function_1(){ List<Double> list = new ArrayList<Double>(); list.add(1.1); list.add(1.2); list.add(1.3); list.add(1.4); Double d = list.remove(0); System.out.println(d); System.out.println(list); } /* * add(int index, E) * 将元素插入到列表的指定索引上 * 带有索引的操作,防止越界问题 * java.lang.IndexOutOfBoundsException * ArrayIndexOutOfBoundsException * StringIndexOutOfBoundsException */ public static void function(){ List<String> list = new ArrayList<String>(); list.add("abc1"); list.add("abc2"); list.add("abc3"); list.add("abc4"); System.out.println(list); list.add(1, "itcast"); System.out.println(list); } }
//迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
System.out.println(str);
}
for(String s : str){
System.out.println(s.length());
}
①
public class EntryDemo { public static void main(String[] args) { Map<Integer,String> map = new HashMap<Integer,String>(); map.put(1, "2ad"); map.put(2, "23d"); map.put(3, "12ad"); Set<Map.Entry<Integer,String>> set = map.entrySet(); Iterator<Map.Entry<Integer, String>> it = set.iterator(); while(it.hasNext()){ Entry<Integer, String> next = it.next(); Integer key = next.getKey(); String value = next.getValue(); System.out.println(key+value); } } }
②
public class EntryDemo {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("2ad", 1);
map.put("23d", 2);
map.put("12ad", 3);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
}
}
}
或者用增强for
for(Map.Entry<String, Integer> entry : map.entrySet()){
System.out.println(entry);
}
注意:增强for不能遍历map,我们上面这个例子是遍历set,通过间接遍历map,不能直接遍历map。
理由:因为Iterable下面没有Map什么事,所以增强for不能遍历map。
注意点:
当我们把String写后面不写前面时候,这样遍历的结果,即使有重复的,也会直接打印输出出来。
此时解决方法:可以在Person类中重写hashCode和equals方法,即可解决出现重复数据的情况。
/* * JDK1.5新特性,静态导入 * 减少开发的代码量 * 标准的写法,导入包的时候才能使用 * * import static java.lang.System.out;最末尾,必须是一个静态成员 */ import static java.lang.System.out; import static java.util.Arrays.sort; public class StaticImportDemo { public static void main(String[] args) { out.println("hello"); int[] arr = {1,4,2}; sort(arr); } }
/* * JDK1.5新的特性,方法的可变参数 * 前提: 方法参数数据类型确定,参数的个数任意 * 可变参数语法: 数据类型...变量名 * 可变参数,本质就是一个数组 */ public class VarArgumentsDemo { public static void main(String[] args) { //调用一个带有可变参数的方法,传递参数,可以任意 // getSum(); int sum = getSum(5,34,3,56,7,8,0); System.out.println(sum); function(1,2,3); } /* * 可变参数的注意事项 * 1. 一个方法中,可变参数只能有一个 * 2. 可变参数,必须写在参数列表的最后一位 */ public static void function(Object...o){ } public static void function(int a,int b,int...c){ } /* * 定义方法,计算10个整数和 * 方法的可变参数实现 */ public static int getSum(int...a){ int sum = 0 ; for(int i : a){ sum = sum + i; } return sum; } }
线程安全
/* * Map集合的嵌套,Map中存储的还是Map集合 * 要求: * 传智播客 * Java基础班 * 001 张三 * 002 李四 * * Java就业班 * 001 王五 * 002 赵六 * 对以上数据进行对象的存储 * 001 张三 键值对 * Java基础班: 存储学号和姓名的键值对 * Java就业班: * 传智播客: 存储的是班级 * * 基础班Map <学号,姓名> * 传智播客Map <班级名字, 基础班Map> */ public class MapMapDemo { public static void main(String[] args) { //定义基础班集合 HashMap<String, String> javase = new HashMap<String, String>(); //定义就业班集合 HashMap<String, String> javaee = new HashMap<String, String>(); //向班级集合中,存储学生信息 javase.put("001", "张三"); javase.put("002", "李四"); javaee.put("001", "王五"); javaee.put("002", "赵六"); //定义传智播客集合容器,键是班级名字,值是两个班级容器 HashMap<String, HashMap<String,String>> czbk = new HashMap<String, HashMap<String,String>>(); czbk.put("基础班", javase); czbk.put("就业班", javaee); //keySet(czbk); entrySet(czbk); } public static void entrySet(HashMap<String,HashMap<String,String>> czbk){ //调用czbk集合方法entrySet方法,将czbk集合的键值对关系对象,存储到Set集合 Set<Map.Entry<String, HashMap<String,String>>> classNameSet = czbk.entrySet(); //迭代器迭代Set集合 Iterator<Map.Entry<String, HashMap<String,String>>> classNameIt = classNameSet.iterator(); while(classNameIt.hasNext()){ //classNameIt.next方法,取出的是czbk集合的键值对关系对象 Map.Entry<String, HashMap<String,String>> classNameEntry = classNameIt.next(); //classNameEntry方法 getKey,getValue String classNameKey = classNameEntry.getKey(); //获取值,值是一个Map集合 HashMap<String,String> classMap = classNameEntry.getValue(); //调用班级集合classMap方法entrySet,键值对关系对象存储Set集合 Set<Map.Entry<String, String>> studentSet = classMap.entrySet(); //迭代Set集合 Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator(); while(studentIt.hasNext()){ //studentIt方法next获取出的是班级集合的键值对关系对象 Map.Entry<String, String> studentEntry = studentIt.next(); //studentEntry方法 getKey getValue String numKey = studentEntry.getKey(); String nameValue = studentEntry.getValue(); System.out.println(classNameKey+".."+numKey+".."+nameValue); } } System.out.println("=================================="); for (Map.Entry<String, HashMap<String, String>> me : czbk.entrySet()) { String classNameKey = me.getKey(); HashMap<String, String> numNameMapValue = me.getValue(); for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) { String numKey = nameMapEntry.getKey(); String nameValue = nameMapEntry.getValue(); System.out.println(classNameKey + ".." + numKey + ".." + nameValue); } } } public static void keySet(HashMap<String,HashMap<String,String>> czbk){ //调用czbk集合方法keySet将键存储到Set集合 Set<String> classNameSet = czbk.keySet(); //迭代Set集合 Iterator<String> classNameIt = classNameSet.iterator(); while(classNameIt.hasNext()){ //classNameIt.next获取出来的是Set集合元素,czbk集合的键 String classNameKey = classNameIt.next(); //czbk集合的方法get获取值,值是一个HashMap集合 HashMap<String,String> classMap = czbk.get(classNameKey); //调用classMap集合方法keySet,键存储到Set集合 Set<String> studentNum = classMap.keySet(); Iterator<String> studentIt = studentNum.iterator(); while(studentIt.hasNext()){ //studentIt.next获取出来的是classMap的键,学号 String numKey = studentIt.next(); //调用classMap集合中的get方法获取值 String nameValue = classMap.get(numKey); System.out.println(classNameKey+".."+numKey+".."+nameValue); } } System.out.println("=================================="); for(String className: czbk.keySet()){ HashMap<String, String> hashMap = czbk.get(className); for(String numKey : hashMap.keySet()){ String nameValue = hashMap.get(numKey); System.out.println(className+".."+numKey+".."+nameValue); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。