//主方法
public static void main(String[] args){}
//属性
int number;
String number;
boolean number;
Customer[] customers;
double number;
char number;
//构造器
public 类名(){}
//带参构造器(this是指当前的属性)
public Customer(String name, char gender, int age, String phone, String email) {
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.email = email;
}
//声明对象传参
Customer customer = new Customer(name, gender, age, phone, email);
//重载构造器(类名相同,形参不同)
public 类名(){}
public 类名(String name){}
//方法
public void emdrMain(){}
//方法重载(方法名相同,形参不同)
public void UinMian(){}
public void UinMian(int age){}
public void UinMian( int age,String opLte){}
//try执行不报错语句,catch执行报错语句
try{ } catch (NullPointerException e){}
//创建声明对象
类名 变量= new 类名();
//修饰符
private(类内部,私有的)、缺省(同一个包)
protected(不同包的子类)、public(同一个工程)
//进行无限循环()
for(;{}
while(){}
//get和set方法
public String getNumber(){
return number;
}
public void setNumber(int number){
this.number =number
}
public class Student extends Person {}
public class ExtendsTest extends Student {}
public class JeieCeop extends ExtendsTest {}
//可变参数(可变参数要放在形参的后面,数组可以放前面)
public class Eoot{
public static void main(String[] args){
Eoot E =new Eoot();
E.say(“1”,“2”,“3”… …)
}
}
public void say(String …str){
System.out.println(str.length)
}
public void (String[] str){
System.out.println(str.length)
}
//四种权限(继承)
//同一个包中的其它类,不可以调用Order类中私有的属性,方法
//在不同的包子类中,不能调用Order类中声明为private和缺省权限的属性方法;
//不同包下的普通类(非子类)要使用Order类,不可以调用声明为private、缺省、protected、权限的属性、方法
//public的权限最大
//super关键字
1.super 理解为父类的
2.super 可以用来调用:属性、方法、构造器
3.我们可以在子类的方法或构造器,通过super属性或super方法的方式,显示的调用
父类中的声明的属性或方法。但是,通常情况下,习惯省略super;
4.特殊情况:当子类和父类中定义了相同的属性时,用super来区分父类属性;
5.在子类调用父类被重写的方法"super.XXX();“,调用重写的方法"this,XXX()”;
6.super 调用构造器:
父类构造器(Person):
int id;
public Person(String name, int age) {
this(name);
this.age = age;
}
public Person() {
System.out.println(“我无处不在!”);
}
子类(Student extends Person):
int id;
public Student(String name,int age,String major){
//"super(形参列表)"的使用,必须声明在子类构造器的首行
super(name,age);//调用父类构造器
this.major=major;
}
public void show() {
System.out.println(“name=” + super.name + “,age=” + super.age+“,major=”+this.major);
//属性id
System.out.println(“id=”+this.id);
System.out.println(“id=”+super.id);
}
public Student() {}
主方法(SuperTest):
public static void main(String[] args) {
Student s1=new Student(“我”,32,“你”);
s1.show();
}
Student mode=new Student();
//数组排序
法一:
int[] arr = new int[]{50, 10, 20, 40, 30};
// int[] arr = {5, 1, 2, 4, 3};
// System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
// System.out.println(arr[i]);
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
System.out.println(arr[i]);
}
法二:
int[] arrr = {3, 2, 4, 1, 5};
for (int j=0;j arrr[i]) {
int apo = arrr[i-1];
arrr[i-1] = arrr[i];
arrr[i] = apo;
}
}
}
//转为字符串形式
System.out.println(Arrays.toString(arrr));
}
//案例(重写)
类一(父类):
package mot.uen;
public class Account {
//属性
private int id;//账号
private double balance;//余额
private double annualInterestRate;//年利率
//带参构造器
public Account(int id, double balance, double annualInterestRate) {
super();
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//返回月利率
public double getMonthlyInterest() {
return annualInterestRate / 12;
}
//取钱
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return;
}
System.out.println("余额不足!");
}
//存钱
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
return;
}
System.out.println("存储金额要大于零!");
}
}
类二(子类):
package mot.uen;
public class CheckAccount extends Account {
//属性
private double overdraft;//可透支限额
private double otor;
//待参构造器
public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
//方法
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public double getOtor() {
return otor;
}
//重写方法
@Override
public void withdraw(double amount) {
if (getBalance() >= amount) {
// setBalance(getBalance()-amount);
super.withdraw(amount);
} else if (overdraft >= amount - getBalance()) {
otor = amount - getBalance();
overdraft -= (amount - getBalance());
setBalance(0);
// super.withdraw(getBalance());
} else {
System.out.println(“超过透支限额,本次交易失败!”);
}
}
}
类三(主方法):
package mot.uen;
public class CheckAccountTest {
public static void main(String[] args) {
// int over=Integer.valueOf(args[0]);
// int over=5000;
CheckAccount acct = new CheckAccount(1122, 20000, 0.035, 4000/,over/);
System.out.println(“第一次消费”);
acct.withdraw(9500);
System.out.println(“您的账户余额为:” + acct.getBalance());
// System.out.println(“您的可透支额度为:”+over);
System.out.println(“您可透支额度为:” + (acct.getOverdraft() + acct.getOtor()));
System.out.println(“这次消费透支额度还剩:” + acct.getOverdraft());
System.out.println();
System.out.println(“第二次消费”);
acct.withdraw(10500);
System.out.println(“您的账户余额为:” + acct.getBalance());
// System.out.println(“您的可透支额度为:”+over);
System.out.println(“您的可透支额度为:” + (acct.getOverdraft() + acct.getOtor()));
System.out.println(“这次消费透支额度还剩:” + acct.getOverdraft());
System.out.println();
System.out.println(“第三次消费”);
acct.withdraw(5000);
System.out.println(“您的账户余额为:” + acct.getBalance());
// System.out.println(“您的可透支额度为:”+over);
System.out.println(“您的可透支额度为:” + (acct.getOverdraft() + acct.getOtor()));
System.out.println(“这次消费透支额度还剩:” + acct.getOverdraft());
}
}
// 如何才能调用子类特有的属性或方法
// 向下转型:使用强制类型转换符
// 使用强转时,可能出现ClassCastException的异常
Person p9= new Man();
//instanceof 报错引入关键字
if(p2 instanceof Woman){
Woman w1=(Woman) p2;
w1.goshopping();
}
//a instanceof A :判断对象a是否是类A的实例,如果是返回true,如果不是返回false
// 为了避免在向下转型出现 ClassCastException的异常,我们在向下转型之前,先
//进行instanaceof的判断,一旦返回true,就继续向下执行,如果返回false就终止执行。
//如果a instanceof A返回true,则 a instanceof B也返回true,其中,类B是类A的父类
if (p2 instanceof Man){
Man m1=(Man)p9;
m1.Ites();
m1.isSmoking=true;
System.out.println(m1.isSmoking);
}
//强制转换,调用子类
Person mo=new Woman();
if (mo instanceof Person){
System.out.println(“^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^”);
Woman r=(Woman)mo;
r.isBeauty=false;
r.eat();
System.out.println(r.isBeauty);
}
多态性的传类调用方法:
package duo.yanghui;
public class InstanceTest {
public static void main(String[] args) {
InstanceTest test = new InstanceTest();
//直接new一个对象,传入method方法的形参里面
test.method(new Graduate());
//初始化Person把变量,传入method方法的形参里面
Person Tte=new Person();
test.method(Tte);
//初始化Student把变量,传入method方法的形参里面
Student Test= new Student();
test.method(Test);
//初始化Graduate把变量,传入method方法的形参里面
Graduate Gra=new Graduate();
test.method(Gra);
}
//多态性的父类(Person)方法,变为形参,可以传子类入参,调用子类方法
public void method(Person eopo) {
//虚拟方法调用
//根据类(eopo)调用getlnfo方法,getlnfo方法 还要看类型;
String info = eopo.getlnfo();
System.out.println(info);
if (eopo instanceof Graduate){
System.out.println("a graduate student");
System.out.println("a student");
System.out.println("a person");
}else if (eopo instanceof Student){
System.out.println("a student");
System.out.println("a person");
}else {
System.out.println("a person");
}
}
}
class Person {
protected String name = “person”;
protected int age = 50;
public String getlnfo() {
return "Name:" + name + "n" + "age:" + age;
}
}
class Student extends Person {
protected String school = “pku”;
public String getlnfo() {
return "Name:" + name + "n" + "age:" + age + "n" + "school:" + school;
}
}
class Graduate extends Student {
protected String major = “IT”;
public String getlnfo() {
return "Name:" + name + "n" + "age:" + age + "n" + "nschool:" + school + "n" + "major:" + major;
}
}
杨辉三角
法一:
public static void main(String[] args) {
//声明并初始化二维数组
int[][] yangHui = new int[10][];
//给数组的元素赋值
for (int i = 0; i < yangHui.length; i++) {
yangHui[i] = new int[i + 1];
for (int j = 0; j < yangHui.length - 1 - i; j++) {
System.out.print(" “);
}
//给首末元素赋值
// yangHui[i][0]=1;
// yangHui[i][i]=1;
//赋值相同,可以赋值为一行(给首末元素赋值)
//给每行的非首末元素赋值
for (int j = 0; j < yangHui[i].length; j++) {
// if(k0||karr[i].length-1){
if (j == 0 || j == yangHui[i].length - 1) {
yangHui[i][0] = yangHui[i][i] = 1;
yangHui[i][yangHui[i].length - 1] = yangHui[i][i] = 1;
System.out.printf(”%4d", 1);
} else {
yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
System.out.printf(“%4d”, yangHui[i][j]);
}
}
System.out.println();
}
}
法二:
public static void main(String[] args) {
//1.声明并初始化二维数组
int[][] arr = new int[12][];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - i; j++) {
System.out.print(" “);
}
arr[i]=new int[i+1];
for (int k = 0; k < arr[i].length; k++) {
arr[i][k]=1;
if(k0||karr[i].length-1){
arr[i][k]=1;
}else {
arr[i][k]=arr[i-1][k-1]+arr[i-1][k];
} System.out.printf(”%4d",arr[i][k]);
}
System.out.println();
}
}
包装类—>基本数据类型
package text1.test2;
import java.util.Scanner;
import java.util.Vector;
public class ScoreTest {
public static void main(String[] args) {
// 1.实例化Scanner,用于从键盘获取学生信息
Scanner scan = new Scanner(System.in);
// 2.创建Vector对象:Vector v=new Vector();
Vector v = new Vector();
// 3.通过for(;;)或者while(true)方式,给Vector中添加数组
int maxScore=0;
for (; ; ) {
System.out.println(“请输入学生成绩(以负数代表输入结束)”);
int score = scan.nextInt();
if (score < 0) {
break;
}
if (score > 100) {
System.out.println(“输入的数据非法,请重新输入”);
continue;
}
// 添加操作
// jdk5.0之前
// jdk5.0之后
v.addElement(score);
// 获取学生成绩的最大值
if (maxScore < score) {
maxScore = score;
}
}
// 4.遍历Vector,等到每个学生的成绩
char level;
for (int i = 0; i < v.size(); i++) {
Object obj = v.elementAt(i);
// jdk5.0之前
Integer inScore = (Integer) obj;
int scors = inScore.intValue();
System.out.println(scors);
// jdk5.0之后
// int scors=(int) obj;
if (maxScore - scors <= 10) {
level = ‘A’;
} else if (maxScore - scors <= 20) {
level = ‘B’;
} else if (maxScore - scors <= 30) {
level = ‘C’;
} else {
level = ‘D’;
}
System.out.println("student-"+i+"score is "+scors+",level is" +level);
}
}
}
单元测试方法(@Test)
package text1.test1;
//Java中的JUnit单元测试
//创建Java类,进行测试
//此时的Java类要求:①此类public ②此类提供公共的无参的构造器
//3.此类中声明单元测试方法
//此时的单元测试方法:方法的权限是public,没有返回参数,没有形参
//4.此单元测试方法上需要声明注释:@Test,并在单元测试类中导入:import org.junit.Test
import org.junit.Test;
public class JUnitTest {
// 面试题
@Test
public void test9() {
Object o1 = true ? new Integer(1) : new Double(2.0);
Object o2 = true ? new String(“1”) : new Double(2.0);
System.out.println(o1);
System.out.println(o2);
}
@Test
public void testA(){
Object o3;
if (true)
o3=new Integer(1);
else
o3=new Double(2.0);
System.out.println(o3);
}
@Test
public void testB(){
Integer i=new Integer(1);
Integer j=new Integer(1);
System.out.println(i==j);//false
Integer m=127;
Integer n=127;
System.out.println(m==n);
Integer x=128;
Integer y=128;
System.out.println(x==y);
}
//String类型--->基本数据类型、包装类:调用包装类的parseXxx()
@Test
public void test8() {
String str1 = "123";
int nun3 = Integer.parseInt(str1);
System.out.println(nun3 + 12);
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
//基本数据类型、包装类--->String类型:调用String重载的ValueOf(Xxx xxx)
@Test
public void test7() {
int num1 = 10;
//方式1:连接运算
String str1 = num1 + "";
System.out.println(str1);
//方式2:
float f1 = 12.3f;
System.out.println(f1);
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(d1);
System.out.println(str3);
}
@Test
public void test3() {
int num1 = 10;
//基本数据类型--->包装类的对象
method(num1);
// 自动装箱:
int num2 = 1980;
Integer in1 = num2;
System.out.println(in1);
// 自动拆箱:
boolean b1 = true;
Boolean b2 = b1;
System.out.println(b2);
// 自动拆箱:包装类—>基本数据类型
System.out.println(in1.toString());
int num3 = in1;//自动拆箱
}
public void method(Object obj) {
System.out.println(obj);
}
//包装类--->基本数据类型:调用包装类的xxxValue();
@Test
public void test5() {
//包装类的对象
Integer in1 = new Integer(12);
int inu1 = in1.intValue();
System.out.println(inu1 + 1);
Float f1 = new Float(12.3);
float f2 = f1.floatValue();
System.out.println(f2 + 12312);
}
//基本数据类型----->包装类;调用包装类的构造器
@Test
public void texe2() {
int num1 = 10;
Integer dom = new Integer(num1);
System.out.println(dom);
Integer int2 = new Integer("12312");
System.out.println(int2.toString());
}
@Test
public void text3() {
Integer in3 = new Integer(122123);
System.out.println(in3.toString());
Float f1 = new Float(12.3f);
Float f2 = new Float("12.3");
System.out.println(f1);
System.out.println(f2);
Boolean b1 = new Boolean(true);
System.out.println(b1);
Boolean b3 = new Boolean("true123");
System.out.println(b3);
}
@Test
public void text4() {
Order order = new Order();
System.out.println(order.inMale);
System.out.println(order.isInMale);
}
}
class Order {
boolean inMale = true;
boolean isInMale = true;
}`
`



