文章目录
稀疏矩阵
package com.test.ljb;
/**
*
* @author 15751083927
* 稀疏矩阵:第一行存矩阵的大小和有效值
* 以后每行存有效值所在位置和有效数
*/
public class Test {
public static void main(String[] args) {
int arr[][] = new int[11][11];
arr[1][2] = 1;
arr[2][3] = 1;
//转换为稀疏矩阵
//获取有效值个数
int sum = 0;
for (int[] is : arr) {
for (int i = 0; i < is.length; i++) {
if (is[i] != 0) {
sum++;
}
}
}
System.out.println("有效值个数是:" + sum);
//创建一个稀疏矩阵的数组
int array[][] = new int[sum+1][3];
array[0][0] = 11;
array[0][1] = 11;
array[0][2] = sum;
//遍历二维数组,将非零的值,存放在稀疏数组中
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
count++;
array[count][0] = i;
array[count][1] = j;
array[count][2] = arr[i][j];
}
}
}
//输出
System.out.println("输出数组");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("%4d",array[i][j]);
}
System.out.println();
}
}
}
面向对象编程
函数方法
package com.ljb.test;
public class Test {
public static void main(String[] args) {
System.out.println();
}
/*
* 修饰符 返回值类型 方法名(....){
* 方法体
* return 返回值
* }
*/
//return 结束方法
public String sayHello(){
return "hello";
}
public int max(int a, int b){
return a > b ? a : b;
}
}
静态方法
package com.ljb.test;
public class Test {
//静态方法
//可以类名.方法名 调用
//非静态方法
//实例化这个类(... new... ) 再调用
}
值传递 引用传递
package com.ljb.test;
//值传递和引用传递
public class Test {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Test.change(person);
System.out.println(person.name);
}
public static void change(Person person){
//person 是一个对象,指向前 Person person = new Person(); 这是一个具体的人,可以改变属性
person.name = "李锦彪";
}
}
//定义了一个Person类, 有一个属性:name
class Person{
String name;
}
构造
main
package com.ljb.test;
public class Application {
public static void main(String[] args) {
//类:抽象的,实例化
//类实例化会返回一个自己的对象
//student对象就是一个Student类的具体实例
Student student = new Student();
student.name = "李锦彪";
student.age = 22;
System.out.println(student.name);
System.out.println(student.age);
//实例化一个对象
// 第一种 Person person = new Person();
//第二种
Person person = new Person("李锦彪");
System.out.println(person.name);
}
}
Person类
package com.ljb.test;
public class Person {
//一个类即使什么都不写,他也会存在一个方法
//显示的定义构造器
String name;
/*
* 实例化初值
* 1.使用new关键字,本质是在调用构造器
* 2.用来初始化值
*/
/*第一种
* public Person(){
* this.name = "李锦彪";
* }
*/
//第二种
public Person(){
}
//有参构造:一旦定义了有参构造,无参就必须显示定义(重载)
public Person(String name){
this.name = name;
}
/*
* 构造器:
* 1. 和类名相同
* 2. 没有返回值
*
* 作用:
* 1. new本质是在调用构造方法
* 2. 初始化对象的值
*
* 注意点:
* 1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造
*/
}
Student 类
package com.ljb.test;
public class Student {
//属性: 字段
String name;
int age;
//方法
public void study(){
System.out.println("this.name 在学习");
}
}
小结
package com.ljb.java;
public class Application {
public static void main(String[] args) {
/*
1.类与对象
类是一个模板:抽象,对象是一个具体的实例
2。方法
定义,调用、
3.对应的引用
引用类型: 基本类型
对象是通过引用来操作的:栈 --> 堆
4.属性:字段Field 成员变量
默认初始化
5。对象的创建和使用
- 必须使用new 关键字创造对象,构造器 Person person = new Person();
- 对象的属性: person.name
- 对象的方法:person.sleep();
6. 类:
静态的属性 属性
动态的行为 方法
*/
}
}
封装
- 该露的露,该藏的藏
我们程序设计要追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。 - 封装(数据的隐藏)
通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。 - 属性私有
package com.ljb.oop;
/*
作用:
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统的可维护增加了
*/
public class Application {
public static void main(String[] args) {
Student s = new Student();
s.setId("2012011131");
s.setName("李锦彪");
s.setSex('男');
System.out.println(s.getId());
System.out.println(s.getName());
System.out.println(s.getSex());
}
}
继承
- extends 是“扩展”的意思,子类是父类的扩展
- Java中只有单继承,没有多继承
super
super注意点
-
调用父类的构造方法,必须在构造方法的第一个
-
必须只能出现在子类的方法或者构造器中!
-
和this 不能同时调用构造方法!
和this比较
- 代表的对象不同
- this:本身调用者这个对象
- super:代表父类对象的应用
- 前提
- this:没有继承也可以使用
- super:只能在继承条件下才可以使用
- 构造方法
- this():本类的构造
- super():父类的构造
父类
package com.ljb.oop; //父类或基类 public class Person { protected String name = "李锦彪"; public Person() { System.out.println("Person无参执行"); } public void print(){ System.out.println("Person"); } }
子类
package com.ljb.oop; //子类或派生类 //子类继承父类,有父类的所有方法 public class Student extends Person{ public Student(){ //隐藏代码:调用了父类的无参构造 super();//调用父类的构造器,必须在子类构造器的第一行 System.out.println("Student 无参执行了"); } private String name = "黄欣宇"; public void print(){ System.out.println("Student"); } public void test1(){ print(); //这类中的 this.print(); // 这两个类中的 super.print(); // 父类中的 } public void test(String name){ System.out.println(name); //调用传入的值(主函数) System.out.println(this.name); //调用这个类中的值 System.out.println(super.name); //调用父类的值 } }
主函数
package com.ljb.oop; public class Application { public static void main(String[] args) { Student s = new Student(); s.test1(); } }
方法的重写
- 需要有继承关系,子类重写父类的方法
- 1.方法名必须相同
- 2.参数列表必须相同
- 3.修饰符:范围可以扩大:public>private
- 4.抛出异常:范围可以被缩小,但不能扩大;
为什么重写
- 父类的功能,子类不一定需要或者不一定满足
重写,子类的方法和父类必须一致;方法体不同
主函数
package com.ljb.oop;
public class Application {
//静态方法和非静态方法区别很大
//静态方法:方法的调用只和左边,定义数据类型有关
//非静态:重写(只能是public)
public static void main(String[] args) {
A a = new A();
a.test();
//父类的引用指向子类
B b = new A(); // 子类重写了父类的方法
b.test();
}
}
类
package com.ljb.oop;
//重写是方法的重写和属性无关
public class B {
public void test(){
System.out.println("B->test");
}
}
package com.ljb.oop;
public class A extends B{
//@Override 重写
@Override //注解:有功能的注释
public void test() {
System.out.println("A->test");
}
}
多态
注意事项
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系,类型转换异常!
- 存在条件:继承关系,方法需要重写,父类引用指向子类对象!
不能重写的方法
- static 方法:属于类,他不属于实例
- final 常量
- private 方法
package com.ljb.demo;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
// new Person():
//可以指向的引用类型就不确定了 父类的引用指向子类
//Student 能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行那些方法,主要看对象左边的类型,和右边没关系
((Student) s2).eat();
s1.run();
s2.run(); //子类重写了父类的方法,执行子类的方法
}
}
instanceof (类型转换)
-
父类引用指向子类的对象
-
把子类转换为父类,向上转型
-
把父类转换为子类,向下转型;强制转换
-
方便方法的调用,减少重复的代码
-
抽象:封装、继承、多态
package com.ljb.study;
public class Application {
public static void main(String[] args) {
//类型之间的转化: 父 子
//高
Person obj = new Student();
//student 将这个对象转换为Student类型,我们就可以使用Student类型的方法
//需要强制转换
Student student1 = (Student) obj;
student1.go();
//子 --- > 父 可能丢失自己本来的方法
}
}
static
package com.ljb.study;
public class Student{
private static int age; //静态变量
private double score; //非静态变量
public void run(){}
public static void go(){}
{ //每次使用对象都调用
//代码块(匿名代码块)
System.out.println("匿名代码块");
}
static { // 只执行一次
//静态代码块
System.out.println("静态代码块");
}
public static void main(String[] args) {
Student s = new Student();
System.out.println(Student.age); //静态变量需要类去访问
System.out.println(s.score); //非静态可是创建访问
new Student().run(); //非静态需要类去调用
go(); // (同一个类下)静态不需要类去调用
}
}
package com.ljb.study;
import static *****; // 静态导入包
public class Student{
public static void main(String[] args) {
System.out.println(Math.random());
}
}
抽象类
package com.ljb.study;
//abstract 抽象类 extends:单继承 (接口可以多继承)
public abstract class Application {
//约束~有人帮我们实现
//abstract,抽象方法,只有方法名,没有方法体
public abstract void doSomething();
//1.不能new 这个抽象类,只能靠子类去实现它:约束!
//2.抽象类中可以写普通的方法
//3.抽象方法必须在抽象类中
//抽象类的所有方法,继承了他的子类,都是必须要实现它的方法
}
接口
- 约束
- 定义一些方法,让不同的人实现
- public abstract
- public static final
- 接口不能被实例化,接口中没有构造方法
- implements 可以实现多个接口
- 必须要重写接口的方法
package com.ljb.study;
//抽象类:extends
//类 可以实现接口 implements 接口
//实现了接口的类, 就需要实现接口的所有方法
public class UserServer implements User, Uerse{
@Override
public void add() {
}
@Override
public void delete() {
}
@Override
public void update() {
}
@Override
public void find() {
}
@Override
public void ha() {
}
}
package com.ljb.study;
//interface 定义的关键字
public interface User {
//常量 public static final
int age = 99;
//接口中所有定义都是抽象的 public
void add();
void delete();
void update();
void find();
}
package com.ljb.study;
public interface Uerse {
void ha();
}
内部类
package com.ljb.study;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过外部类实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
//没有名字初始化类,不能将实例保存到变量中
new Outer().out();
//匿名内部类
User u = new User(){
@Override
public void add() {
}
};
}
}
interface User{
void add();
}
package com.ljb.study;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
//局部内部类
class Inners{
}
}
public class Inner{
public void in(){
System.out.println("这是内部类");
}
//非静态可以获得外部类的私有属性
//静态不可以获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
}
//一个Java类中可以有多个class类,但是只能有一个public
class Inn{
}
抛出异常
package com.ljb.study;
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 0;
//假设抛出多个异常, 从小到大
try{ //监控区域
if (b == 0){
throw new ArithmeticException(); // 主动抛出异常
}
System.out.println(a/b);
}catch (Error e){
System.out.println(e);
} catch (Throwable e){ // 捕获错误
System.out.println("出现异常" + e);
e.printStackTrace(); //打印错误的栈信息
}finally { //处理善后
System.out.println("finally");
}
try {
new Test().test(a, b);
} catch (Exception e) {
e.printStackTrace();
}
}
//方法上抛出异常
public void test(int a, int b) throws Exception{
System.out.println(a/b);
}
}
自定义异常
package com.ljb.study;
public class Test {
//可能存在异常的方法
static void test(int a) throws MyException {
System.out.println("传递的参数为:" + a);
if (a > 10){
throw new MyException(a);
}
System.out.println("ok");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException" + e);
}
}
}
----------------------------------------------------------
package com.ljb.study;
//自定义异常
public class MyException extends Exception{
//传递数 > 10
private int detail;
public MyException(int a){
this.detail = a;
}
//toString :异常的打印信息
@Override
public String toString() {
return "MyException{" +
"detail<" + detail +
'}';
}
}
xception {
System.out.println(“传递的参数为:” + a);
if (a > 10){
throw new MyException(a);
}
System.out.println(“ok”);
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException" + e);
}
}
}
package com.ljb.study;
//自定义异常
public class MyException extends Exception{
//传递数 > 10
private int detail;
public MyException(int a){
this.detail = a;
}
//toString :异常的打印信息
@Override
public String toString() {
return "MyException{" +
"detail<" + detail +
'}';
}
}