javaseDay_10

来源:互联网 发布:linux搭建lamp服务器 编辑:程序博客网 时间:2024/05/23 23:50
Object类:
类Object是类层次结构的根类.每个类都使用Object作为超类(父类).所有对象(包括数组都实现这个类的方法.
public int hashCode()返回该对象的哈希码值
     hashCode()-通过哈希算法(哈希表:hashtable)-地址值(不是实际意义上的地址!)
public final Class getClass()返回此Object的运行时类
Class类中有一个方法:
Public String getName()以String的形式返回此Class对象所表示的实体(类,接口,数组类,基本数据或void)名称.
 
Eg:
class Student extends Object{
}
public class ObjectText {
 public static void main(String[] args) {
Student s1=new Student();
System.out.println(s1.hashCode()); //869295101
Student s2=new Student();
System.out.println(s2.hashCode());//911511966
System.out.println("hello".hashCode()); //99162322
    Class c1=s1.getClass(); 
//public String getName():获取当前正在运行的这类的全路径名称!
String name = c1.getName() ;
System.out.println("name:"+name);// name:day10.Student}

}
}
Public final class getClass()  //返回此Object的运行时类.


Object的另一个方法:
public String toString();返回该对象的字符串表示(建议所有子类都重写此方法)
Integer类中有一个方法:
   public static Stri ng toHexString(int i); 将一个int类型的数据转换成一个十六进制的字符串表现形式.
如果直接输出对象名称,想要显示当前对象的成员变量的值,那么必须重写Object类中的toString()方法.


Eg:
public class ObjectDemo {
    public static void main(String[] args) {
Student s1=new Student("龙哥",20);
System.out.println("s1:" +s1);
System.out.println("toString():"+s1.toString());
System.out.println(s1.getClass().getName()+"@"+Integer.toHexString(s1.hashCode()));
//直接对象名称,想要显示成员变量的值,怎么办?
System.out.println(s1);
Student s2 = new Student("龙哥", 20) ;
System.out.println("s2:"+s2);
System.out.println(s1==s2);  //比较的是地址值
    }
 }
输出结果:
s1:Student [name=龙哥, age=20]
toString():Student [name=龙哥, age=20]
day10.Student@4ec4d412
Student [name=龙哥, age=20]
s2:Student [name=龙哥, age=20]
false


Object中的另一个方法:
public boolean equals(Object obj)判断某个对象是否和他”相等”


面试题:
==和equals()方法的区别?
==:比较的是两个对象的地址值是否相同,
equals()方法默认比较的是两个对象的地址值是否相同,如果重写Object类中的equals()方法,则默认的是比较两个方法的内容是否相等.
Eg:
public class ObjectDemo1 {
public static void main(String[] args) {
Student s1=new Student("龙哥",20);
Student s2=new Student("龙哥",20);
System.out.println(s1==s2);
System.out.println("上面地址的比较"); 
Student s3=s1;
System.out.println(s1==s3);
System.out.println("s1和s3指向的内存相同");
}
}
 如果要equals比较两个的内容,必须重写equals方法.
Object类的equals()方法底层是通过”==”来实现的,所以默认比较的是地址值.


Object类中的其他两个方法:
Protected void finalize()throws Throwable:当垃圾回收器确定不存在对该对象的更多引用时
由对象的垃圾回收期调用此方法.但是调用的时间 不确定.
System类中的一个方法:
public void gc():运行垃圾回收器,这个垃圾回收器最终调用的就是finalize()方法.
protected Object clone()创建并返回此对象的一个副本.
  throws CloneNotSupportedExpection
注意事项:
Object 类的clone方法执行特定的复制操作.首先,如果此对象的类不能实现接口Cloneable,则会抛出 CloneNotSupportedException异常.
Eg:
public class ObjectDemo2 {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("龙哥");
s1.setAge(20);
System.out.println(s1.getName()+"  "+s1.getAge());
   //复制s1这个对象
// Object obj=s1.clone();
// Student  s2=(Student)obj; //向下转型
// System.out.println(s2.getName()+"   "+s2.getAge());
    //将s1的地址值赋值给s3对象,s3指向s1的堆内存地址值
Student s3=s1;
System.out.println(s3.getName()+"  "+s3.getAge());
}
}
//要去使用clone()方法,当前对象所在的类一定要实现cloneable接口,


Scanner:
1. Scanner:创建一个文本扫描器(键盘录入)
2. Java高级特性:IO流
BufferReder:字符缓冲流来键盘录入
Import Java.util.Scanner; 
Scanner sc=new Scanner(System.in);
System类中的静态字段:
public static final InputStream in:标准输入流
InputStream:字节流  InputStream is=System.in;
实质:抽象类多态
public static final OutputStream out:标准输出流
开发步骤:
1. 创建键盘录入对象
2. 录入数据
3. 输出
Eg:
import java.util.Scanner;
public class ScannerDemo1 {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc=new Scanner(System.in);
//录入数据
System.out.println("请输入数字");
int a=sc.nextInt();
System.out.println("a:" +a);
}
}
Scanner类中常用的方法:
判断的功能:
细节:可以添加逻辑判断
hasNextXXX();在录入数据之前,加上判断功能,判断是否由下一个可以录入的XXX类型的数据.
nextXXX();//通过录入获取这个int类型的数据.
nextInt():录入int类型的数据
nextLine():录入一个字符串的类型
java.util.InputMismatchException:  //异常原因:输入和想到的数据不匹配
eg:
public class ScannerDemo2 {
public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   if(sc.hasNext()){
  //是否由下一个可以录入的int类型的数据
  int a=sc.nextInt();
  System.out.println("a:"+a);
   }else{
  System.out.println("录入数据类型和想要的不匹配");
   }
}
}
如果输入字符类型 则会报java.util.InputMismatchException:异常.


Scanner类中的注意事项:
先录入int类型的数据,在录入String类型数据,第二次录入的数据没有接受到,直接输出结果
由于回车换行符导致的; 回车符号属于String类.
解决方案:第二次录入String类型数据之前,重新创建键盘录入对象录入String类
Eg:
import java.util.Scanner;
public class ScannerDemo3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();   //录入两个int类型的数据
int b=sc.nextInt();
System.out.println("a:"+a+ "b:"+b);
    String c=sc.nextLine();
    String d=sc.nextLine();


//先录入int类型,再录入String类型
    int e = sc.nextInt() ;
    Scanner sc2 = new Scanner(System.in) ;
String f= sc2.nextLine() ;
System.out.println("e"+e +"f"+f);
}
}


要求:模拟用户登录,给出三次机会,并给出提示
import java.util.Scanner;
public class ScannerText2 {
public static void main(String[] args) {
String name="longge";   //定义用户名和密码
String password="longge";
for(int x=3;x<3;x++){
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名");
String newUserName=sc.nextLine();
System.out.println("请输入密码");
String newpassword=sc.nextLine();
//判断用户名和密码是否正确
    if(name.equals(newUserName)&&password.equals(newpassword)){
    System.out.println("登录成功开始玩游戏");
    GuessNumberGame.start() ;
    break;
    //加入一个类.类里方法为猜数字游戏
    }else{
    if((2-x)==0){
    System.out.println("机会用完,请联系管理员");
    }else{
    System.out.println("你还有"+(2-x)+"次机会");
    }
    }
}
}
}
猜游戏的: import java.util.Scanner;
public class GuessNumberGame {
private GuessNumberGame(){
}
public static void start(){
int number=(int) (Math.random()*101);
    int count=0;
    while(true){   //由于需要多次录入,所以使用while(true)
    Scanner sc=new Scanner(System.in);
    int guessNumber=sc.nextInt();
    count++;
    if(guessNumber>number){
    System.out.println("您输入的数据"+guessNumber+"大了");
    }else if(guessNumber<number){
    System.out.println("您输入的数据"+guessNumber+"小了");
    }else{
    System.out.println(count+"次猜中了");
         break;  
    }
    }
}
}




String:
代表字符串,Java程序中所有的字符串字面值(如”abc”)都作为此类的实例实现;
特点:字符串一旦被赋值:值不会改变


String类常用的构造方法:
String():表示一个空字符序列
public String(byte[]bytes,Charset ch):默认字符集(编码格式):GBK,如果是GBK格式,可以不写第二个参数.因为默认.
Public String(byte[]bytes,int index,int length):将部分字节数组构成一个字符串
public String(char[]value):将字符数组构造成一个字符串
public String(char[]value,int index,int length):将部分字符数组构造成一个字符串.
public String(String orginal):通过字符常量构造一个字符串对象
获取字符串的长度功能:public int length()


面试题:数组中有没有length(),字符串(字符串缓冲区:StringBuffer)有没有length(),集合中有没有length()?
数组中没有length(),数组中为length属性.
字符串中有length()
集合中没有length(),获取集合中元素数量:size()


编码和解码:一定要保证编码格式一致
编码:把能看懂的东西转换成一个看不懂的东西:
String-byte[]:public byte[]getBytes(string charseName)
解码:吧当前的byte[]转成能看懂的东西(
String: byte[]--->String:public String(byte[]bytes,CharsetName ch)
Eg:
 import java.util.Arrays;
public class StringDemo {
public static void main(String[] args) {
   String s1=new String(); 
   System.out.println("s1.length():"+s1.length());
   System.out.println("s1:"+s1);
 //public String(byte[] bytes,Charset ch):默认字符集(编码格式):GBK,如果是GBK格式,可以不写第二个参数
 //创建一个字节数组
   byte[]arr={89,76,65,103,99,126};
   String s2=new String(arr);//字节的值找它对应的ASCII码表中的值
   System.out.println("s2:"+s2);
   System.out.println("s2.length():"+s2.length());
//编码解码
   String a="世界";
//   byte[]arr2=a.getBytes("utf-8");
   byte[]arr2=a.getBytes();
   System.out.println(arr2);
   System.out.println(Arrays.toString(arr2));
System.out.println("--------------");
//public String(char[] value):将字符数组构造成一个字符串
char[]arr3={'我','是','吴','龙','龙'};
System.out.println("arr3.length()"+arr3.length);
System.out.println("arr3"+arr3);
System.out.println("---------");
//public String(String original):通过字符串常量构造一个字符串对象
String arr5=new String("world");
System.out.println("arr5"+arr5);
System.out.println("arr5.length:"+arr5.length());
String arr6="world";
System.out.println("arr6"+arr6);
System.out.println("arr6.length():"+arr6.length());
}
}
输出结果:
s1.length():0
s1:
s2:YLAgc~
s2.length():6
[B@28084850
[-54, -64, -67, -25]
--------------
arr3.length()5
arr3[C@37c390b8
---------
arr5world
arr5.length:5
arr6world
arr6.length():5
eg: 编码:”我”--字节数组:byte[]字节类型:形成一种二进制数据
解码:二进制数据--十进制数据--String ”我”


重要特点:
字符串一旦被赋值,其值不能再改变(是不可变的字符序列)
面试题
String s=”hello” 与String s=new String(“hello”)的区别?分别创建了几个对象
第一个创建一个对象 在字符串常量池中找 如果有引用,没有创建
第二个创建了两个对象 首先在栈内存开辟空间,堆内存保存值,并且字符串常量池中也有这样一个字符串常量.


public class StringDemo4 {
public static void main(String[] args) {
String s="world";
System.out.println("s:" +s);
change(s);
System.out.println("s:"+s); 
}
public static void change(String s){
s +="InInternet";
}
}
//change方法不会改变字符串s的值 .一旦被赋值 无法改变
String 类型作为形参和基本数据类型的效果相同
输出结果:
s:world
s:world
课堂练习:看程序写结果
如下public class StringDemo2 {

public static void main(String[] args) {
//创建字符串对象
String s1 = new String("hello") ;
String s2 = new String("hello") ;

System.out.println(s1==s2);

String s3 = "hello" ;
String s4 = "hello" ;
System.out.println(s3==s4);
System.out.println(s3.equals(s4));

String s5 = new String("world") ;
String s6 = "world" ;
System.out.println(s5==s6);
System.out.println(s5.equals(s6));
}
}
输出结果为:
false//s1与s2比较的是地址值
true// s3与s4都指向在String常量池里的hello
true//s3.equals(s4));指向的地址相同
flase//s5指向两个地址 s6只指向常量池中的地址
true//s5与s6的内容相同




String类的过去功能:
Int length


String类的获取功能
Int length():获取字符串长度的功能
char charAt(int index):返回的是索引处对应的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
int indexOf(String str):返回指定字符串在此字符串中出现的第一次出现的索引
int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索.
Int indexOf(String str,int fromIndex):返回此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜素.
String substring(int start):从指定位置开始截取字符串,默认截取到末尾.
String substring(int start,int end):从指定位置开始截取到指定位置结束,包前(start索引)不包后(end索引)


举例:
public class StringDemo5 {
public static void main(String[] args) {
String chr="helloworld";
System.out.println("length:"+chr.length());//获取长度
//char charAt(int index):返回的是索引处对应的字符
System.out.println("charAt:"+chr.charAt(8));
System.out.println("---");
//int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
System.out.println("indexof"+chr.indexOf('5'));
//如果要查的值没有 输出的值为-1
    //int indexOf(String chr:返回指定在此字符串中第一次出现的索引
System.out.println("indexOf:"+ "owo");
    System.out.println("-------");
    //String substring(int start):从指定位置开始截取字符串,默认截取到末尾.且返回一个新的字符串.不是它本身
    System.out.println("subString"+chr.substring(5));
    //String substring(int start,int end):从指定位置开始到指定位置结束:注意:包前不包后
    System.out.println("substring:"+chr.substring(3,6));
}
}
输出结果:
length:10
charAt:l
---
indexof-1
indexOf:owo
-------
subStringworld
substring:low




字符串的遍历:使用for循环遍历,String类的length()和charAt()相结合
public class StringDemo7 {
public static void main(String[] args) {
String a="goodgoodstudy";
for(int x=0;x<a.length();x++){
System.out.print(a.charAt(x)+" ");
}}
}


练习:将数组中的数据按照指定个格式拼接成一个字符串举例
分析:定义数组.定义空字符串.用空的字符串拼接”[” 
最后遍历int类型的数组,获取每个元素 查看当前索引是否为最大索引.
如果是:用空串拼接 arr[x],用空串拼接”]”
如果不是:用空串+=arr[x],用空串拼接”,”


public class StringText {
public static void main(String[] args) {
int []arr={3,5,9};
String result=arrayToString(arr);
System.out.println("result:"+result);
}
private static String arrayToString(int[] arr) {
String result="";
result+="[";
for(int x=0;x<arr.length;x++){
if(x==arr.length-1){
result+=arr[x];
result+="]";
}else{
result+=arr[x];
result+=",";
}
}return result;
}
}
练习题:使用键盘录入一个字符串:统计该字符中大写字母字符,小写字母字符,数字字符出现的此数
//分析:定义三个统计变量;键盘录入对象 遍历字符串.if判断语句 变量++


import java.util.Scanner;
public class StringText3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一行字符串");
String str=sc.nextLine();
int Bigcount=0;
int Smallcount=0;
int Numbercount=0;
for(int x=0;x<str.length();x++){
char ch = str.charAt(x);//获取每个字符
if(ch>='a'&&ch<='z'){
Smallcount++;
}else if(ch>='A'&&ch<='Z'){
Bigcount++;
}else if(ch>='0'&&ch<='9'){
Numbercount++;
}
}
System.out.println("大写字符有"+Bigcount+"个");
System.out.println("小写字符有"+Smallcount+"个");
System.out.println("数字字符有"+Numbercount+"个");
}
}
输出结果: 请输入一行字符串
gffgfggf26hf3
大写字符有0个
小写字符有10个
数字字符有3个


String类的转换功能:
byte[]getbyte():将字符串转换字节数组;
char[]toCharArray():将字符串转换成字符数组(常用)
static String valueOf(char[]chs):将字符数组转换成字符串   //外面是要转换的类型,里面是原来的类型
String toLowerCase():将字符串全部转换成小写;
String toUpperCase():将字符串全部转换为大写;
String concat(String str):字符串拼接方法


例子:
public class StringText8 {
public static void main(String[] args) {
String b="haoduoya";
//byte[]getBytes():将字符串转换为字节数组
byte[]bys=b.getBytes();
//遍历字节数组
for(int x=0;x<bys.length;x++){
System.out.println(bys[x]);
}
    //char[]toCharArray():将字符串转换成字符数组
char[]ays=b.toCharArray();
for(int x=0;x<ays.length;x++){
System.out.println(ays);
}
//static String valueOf(char[]bys将字符数组转换成字符串
     String b1=String.valueOf(ays);
     System.out.println("b1:"+b1);
   //static String valueOf(int i):将一个int类型的数据转换成字符串
    String b2 = String.valueOf(300) ;
    System.out.println("s3:"+b2);
  //String toLowerCase():将字符串全部转成小写
  System.out.println("toLowerCase:"+b.toLowerCase());
  //String toUpperCase():将字符串全部转换成大写
  System.out.println("toUpperCase:"+b.toUpperCase());
 
  //String concat(String str):字符串拼接方法.
  String a1="dade";
  String a2="youdianlei";
  System.out.println("contact:"+a1.concat(a2));
      
  String a3="made";   //字符串拼接的另一种
  a3+="yitianwanle";
  System.out.println("a3:"+a3);
}
}


输出结果:
1049711110011711112197      
haoduoyahaoduoyahaoduoyahaoduoyahaoduoyahaoduoyahaoduoyahaoduoyab1:haoduoya
    ---  
s3:300
    ---  
toLowerCase:haoduoya
    ---  
toUpperCase:HAODUOYA
    ---  
contact:dadeyoudianlei
    ---  
a3:madeyitianwanle




递归


首先构造方法不能使用递归:
方法递归:方法调用本身的一种现象
递归的三个条件:
1. 需要定义某个方法
2. 方法必须有出口条件
3. 必须有某一种规律


public class StringDemo3 {
public static void main(String[] args) {
/*
*求5的阶乘
*5!=5*4; 
* */
int jc=1;
for(int x=2;x<=5;x++){
jc*=x;
}
System.out.println("5的阶乘是:"+jc);//普通方法
     System.out.println("6的阶乘是 "+jieCheng(6));
}
public static int jieCheng(int n){
if(n==1){
return 1;
}else{
return n*jieCheng(n-1);
}
}
}


使用递归要注意:明确返回值类型:
Int类型  
参数类型:int 类型的值
3. 出口条件:
If(n=1){
Return 1;
}
4. 要有规律
If(n!=1){
Return  1;
}else  {
Return n*jieCheng(n-1);
}
}
}
原创粉丝点击