#我的android学习

java的面向对象基础

类的定义方法

类名,成员变量,成员函数(方法)

class Person //类名
{
int age; //成员属性
void shout() //成员函数(方法)
{
System.out.println(“oh,my god!i am”+age);
}
}

 生成对象的方法
 格式:类名 对象名=new 类名();
 例如 Dog dog =new Dog();
     Dog d =new Dog();
     **一旦生成对象一定要用new关键字**
    (多个对象  Dog d1= new Dog();
    		   Dog d2 = new Dog();
     )

函数的重载

函数的名字相同,但是所有的函数功能(行为)是不同的。
void A();
void A(int i);
void A(int i,double x);

*注意 A(); 是指构造函数 //类似于 Dog d=new Dog();

构造函数的重载
Person(){
}
Person (String n,int a)
{name=a ;
age=a;}
string name;
int age;}

this的使用方法

1.

this在调用的时候,就相当于调用函数的某个对象, 对象在调用这个函数时,相当于是this的实参
class Person{
String name;
void talk(){
Sout(“my name is”+ this.name); //这里的this可省略
}
}
class Test{
public static void main(String args[]){
Person p1 =new Person();
p1.name =“zhangsan”;

Person p2 =new Person();
p2.name =“lisi”;
p1.talk();
p2.talk();
}
}
this.name =name;
this.age=age;

2.

this表示在此函数中调用本类当中的另一函数,减少重复的代码。
Person (String name,int age){
this.name= name;
this.age=age;
}
Person(String name,int age,String address)
{
this(name,age); //避免了使用重复的代码,且这行带有this的代码必须是函数第一行的代码
this.address =address;
}

继承

super(name,age);

覆写override

super.introduce();

//覆写父类的方法,真的很重要
(这两个部分真的爆炸,疯狂忘了保存)

转型

  • 向上转型:将子类的对象赋值给父类的引用
    Student s =new Student();
    Person p= s; //即父类可以引用子类,一个引用能够调用哪些成员(变量和函数),取决于这个引用的类型。
  • 向下转型 :将父类的对象赋值给子类的引用
    Student s1=new Student;
    Person p= s1; //先向上转型
    Student s2 =(Student) p; //再强制转换为向下转型

抽象类与抽象函数

抽象函数:只有函数的定义,没有函数体的函数被称为抽象函数; //没有大括号{},即没有函数体
abstract void fun():

**抽象类:**使用abstract定义的类被称之为抽象类;

  • 1.抽象类不能生成对象
  • 2.如果一个类中包含抽象函数,那么这个类 必须
    被声明为抽象类;
  • 3.如果一个类中没有抽象函数,那么这个类也可以被声明为抽象类。
    所以抽象类天生是当爹的
    因为为,在抽象类中的抽象函数中,在子类中要详细的定义。
    那么?!!为什么需要有抽象函数呢
    1.提醒子类要覆写特定的函数

包和权限的访问

软件包:package,相当于一个文件夹。
package 把类放在一个包中 ,将类放置到一个包中,需要使用package“包名”()编译时需要使用-d参数,作用是依照包名生成相应的文件夹。
包名的命名规范:
1.要求包名所有的字母都要小写
2.包名一般情况下,是你的域名倒过来写

权限有 public、private(私有,只能在该类中使用,在同一个包中也无法使用)、default(如果在子类和父类不在同一个包中,则子类可以继承到父类的default权限的成员变量和函数书,是因为权限不够,无法使用)、protected
而要使用另一个包中的类,需要使用导入import

接口的基础

什么是接口?

  • 使用interface定义
  • 接口当中的方法都是抽象方法
  • 接口当中的方法都是public权限
    使用implements时关键字,是实现接口,因为我接口都是抽象类,所以需要复写父类来继承接口,即 implements
与继承不同的是:
  • 实现接口使用implements
  • 一个类可以实现多个接口
  • 一个接口可以继承多个接口

接口的应用
例如有一个打印机

interface Printer{
	publid void open();
	public close();
	public void print (string s);
} //接口
class CanonPrinter implements*Printer{   //继承父类的子类的实现接口
	private void clean(){
		System.out.println("clean");
		}
	public void close(){
		this.clean();
		System.out.println("close");
	}
	
	public void print(string s){
		System.out.println("Canon print ---> " + s);
	}
}
classs test{    //测试函数
	public static void main(string args []){
		Printer printer =null;
		Printer printer = new CanonPrinter();
		printer.open();
		printer.print("test");
		printer.close();
}
}

异常

异常的分类:
在这里插入图片描述
**如何处理异常?
try catch的结构

try{   }
catch{  }**

try{
	int i =1/0;
}      //把有可能出现异常的代码放到try里面
catch{
	e.printStackTrace();  //打印异常的信息
}
**若try代码里没有异常,则Catch里的代码不执行**
 
也有try{
//打开文件的代码
} 
catch{

} 
finally{
//关闭文件的代码
}
与异常相关的关键字Throw、Throws
class User{  	
private int age;  
	public void setAge(int age) throw Exception{ 		
		if(age<0){
		Exception r =new Exception("年龄不能为负数");
			throw e; 		
			} 
			this.age =age;
	} }

class Test{
public static void main(String args [ ]){
User user= new User();
try{
user.setage(-20);
}
catch(Exception e){
System.out.println(e);
}
}
}

  • Throws在调用函数时使用
  • 而Throw则是在java无法判断出这是一个异常时,将异常抛出让java虚拟机知道这是一个异常。

I/O流

即Input跟Output,流是指一大组数据通过管道来进行输入跟输出的流

IO的分类在这里插入图片描述

例:
创建两个文件,I文件跟O文件(字节流的使用)
在test.Java中测试

import java.io.*;   //导入类

class Test{
	public static void main(String args [ ] ){
		FileInputStream fis = null;  //声明输入流的引用
		try{
			fis =new FileInputStream("e:/src/i.text");
			//生成代表输入流的对象
			fos =new FileOutputStream("e:/src/o.text");
			//生成输出流的对象
			byte [ ] buffer =new byte [100]; 
			//生成一个字节数组
			fis. read(buffer,5,buffer.length-5); 
			//调用输入流对象的read方法,读取数据,最后减去5防止数组越界
			fos.write(buffer.5.buffer.length)
			for(int i=0;i<buffer.length;i++);
			System.out.println(buffer[i]); 
			//输出数字
}
catch(Exception e){
			System.out.println(e);
}
}
}

在这里利用了buffer这个数组的中转站来存储小文件

(其中:拓展 read write的方法在这里插入图片描述

如何输入输出大文件呢? 用到了字符流

字符流:读写文件时,在字节流的前提下,以字符为基础
字节输入流:Reader <-- FileReader
字节输出流:void write (char [ ] c,int off ,int len)

class Test{
	public static void main(String args[ ]){
		FileInputStream fr =null;
		FilePutputStream fw =null;
		try{
			fr=new FileInputStream("e:src/i.text");
			fw =new FileOutputStream("e:/src/o.text");
			char buffer [ ] = new char[100];
			int temp = fr.read(buffer);
			fw.write(buffer,0,temp);}
			
	catch(Exception e){
	System.out.println(e);
}
finally{
fr.close();
fw.close();
}
}
}
节点流,处理流

装饰者模式BufferedReader
publicStringreadline()
throwsIOException
例如:水管工模式

interface Worker{
		public void doSomeWorker();
}
class Plumber implements Worker{
	public void doSomeWork(){
		System.out.println("修水管");
}
}
class Carpenter implements Worker{
	public void doSomeWork(){
		System.out.println("修门窗");
}
}
class Aworker implements Worker{  
//A公司的worker
		private Worker worker ;
		public AWorker(Worker worker ){
	this.worker =worker ;
}
		public void doSomeWork(){
		     System.out.println("你好");
		     worker.doSomeWork();
}
}
class Test{
  	public static void main(String args[ ] ){
		Plumer plumer =new Plumer ();
		AWorker awoker = new AWorker(plumber);
		aworker1.doSomeWorker();
		
		CApenter capenter =new CApenter();
		AWorker aWorker2 =new AWoker(capenter);
		awoker2.doSomeWork();
}
}

内部类

定义:
class A{
class B{
}
}
//在B当中可以使用A的变量,函数

例:
class Test{
public static void main(args [ ]){
A a =new A();
A.B b=new A().new B();
}
}

匿名内部类

即没有名字的内部类

B b =new B();
b.fun(new A(){
public void doSomething(){
System.out.println(“匿名内部类”);
}
})

多线程与多进程

多进程:在操作系统中能同时运行多个任务
多线程:再同意应用程序中有多个顺序流同时执行

创建线程的方法:1 定义一个线程类,他继承类Thread 并重写 其中的方法run(),方法run()成为线程体;

例:

class FirstThread extends Thread {
	public void run(){
	for(int i=0;i<100;i++)
	{
			System.out.println("FirstThread - ->"+i);
}
}        //自己的线程
}
class Test{     //主线程
	public static void main(args[ ]){
		FirstThread ft =new FirstThread(); //启动线程  ft为线程对象,代表了一个线程
		ft.start(); //就绪状态 
		for(int i=0;i<100;i++)
		System.out.println("main -- >"+i);
}
}自己的线程跟主线程的顺序是不确定的

多线程的情况不确定

实现线程的方法 2. 提供一个实现Runnable的类,作为线程的目标对象,在初始化一个Thread类或者Thread子类的线程对象时,把目标对象传递给这个线程实例,由该目标对象提供线程体。

class Runnablelpml implements Runnable{
		public void run(){
		for(int i=0;i<100;i++)
		System.out.println("Runnable -- >"+i);
}
}
//启动线程
class Test{
pubilic static void main(String args[ ] ){
		RunnableImpl ri =new RunnableImpl();
		Thread t =new Thread(ri);  //要把参数传进去 ,生成一个Thread对象,并将Runnable接口实现类的对象作为参数,传递给Thread对象
		t.strat();
}}

//推荐第二种用法,更常用

线程的简单控制方法在这里插入图片描述

class Test{
	public static void main(String args [ ]){
			RunnableImpl ri =new RunnableImpl();
			Thread t =new Thread(ri);  //传参数
			//线程的最大优先级是10,最小优先级是1,优先级越大,代表执行的概率越大,是概率并不一定执行。
			t.setprioirity(Thread.MAX_PRIORITY);
			t.sleep(1000);
			t.start();
	 }
}
多线程的同时进行
  • 交替执行

synchronized(this){
//同步代码块
}

先执行在同步代码块的线程,再等到此线程执行完了后,再执行其他线程。

类集框架

1.类集框架是一组类和接口
2.位于java.util包当中(要导包进来)
3.主要用户存储和管理对象 //来存放一些大量的数据
4.主要分为三大类—集合、列表和映射

集合(set)??
集合中的对象不按特定的方式排序,并且没有重复对象,即是无序的存放。
列表??(List)
集合中对象按照索引位置排序,可以有重复的对象

映射??(Map)
集合中的每一个元素对应一个键对象和一个值对象,键(key)不可以重复,值(value)可以重复.

类集框架与数组的区别:
数组只能在声明的有限长度下,才能放下
而类集框架可以在“无限”的情况下,放置数据。

类集框架的主体结构
类集框架主体结构

import java.util.List;
import java.util.ArrayList;

public class Test{
	public static void main(String args[ ]){
		ArrayList<String> arrayList =new ArrayList<String>;
		arrayList.add("a");    //添加元素
		arrayLiist.remove(0);     //去除元素
		String s = arrayList.get(1);   //取出元素,此时为数组下标越界
		System.out.println(s);
}
}

关键字:
//寒假所学java内容,日常自闭完结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值