1、继承中的构造方法
/**
* Created by apple on 20/5/1.
*/
public class TestSuperSub {
public static void main(String[] args) {
//SubClass c1 = new SubClass();
SubClass c2 = new SubClass(400);
}
}
class SuperClass{
private int n;
public SuperClass(){
System.out.println("父类无参数的构造方法");
}
public SuperClass(int n){
System.out.println("父类有参数的构造方法" + n);
this.n = n;
}
}
class SubClass extends SuperClass{
private int n;
SubClass(){
super(300);
System.out.println("子类无参数构造方法");
}
SubClass(int n){
System.out.println("子类有参数的构造方法" + n);
}
}
输出:
父类无参数的构造方法
子类有参数的构造方法400
/**
* Created by apple on 20/5/1.
*/
public class TestSuperSub {
public static void main(String[] args) {
SubClass c1 = new SubClass();
//SubClass c2 = new SubClass(400);
}
}
class SuperClass{
private int n;
public SuperClass(){
System.out.println("父类无参数的构造方法");
}
public SuperClass(int n){
System.out.println("父类有参数的构造方法" + n);
this.n = n;
}
}
class SubClass extends SuperClass{
private int n;
SubClass(){
super(300);
System.out.println("子类无参数构造方法");
}
SubClass(int n){
System.out.println("子类有参数的构造方法" + n);
}
}
输出:父类有参数的构造方法300
子类无参数构造方法
/**
* Created by apple on 20/5/1.
*/
public class TestStudent {
public static void main(String[] args) {
PersonP p1 = new PersonP("A");
PersonP p2 = new PersonP("B","Shanghai");
StudentP s1 = new StudentP("C","S1");
StudentP s2 = new StudentP("D","SHENZHEN","S3");
System.out.println(p1.info());
System.out.println(p2.info());
System.out.println(s1.info());
System.out.println(s2.info());
}
}
class PersonP{
private String name;
private String location;
PersonP(String name){
this.name = name;
location = "Beijing";
}
PersonP(String name,String location){
this.name = name;
this.location = location;
}
public String info(){
return name + location;
}
}
class StudentP extends PersonP{
private String school;
StudentP(String name,String school){
this(name,"shanghai",school);
}
StudentP(String name, String location,String school){
super(name,location);
this.school = school;
}
public String info(){
return super.info() + school;
}
}
输出:
ABeijing
BShanghai
CshanghaiS1
DSHENZHENS3
根据前面的程序,构造Teacher 类,继承Person类,要求,
1、增加:职称String属性
2、具有和Student类类似的重载构造方法
3、重写Person类的info方法。增加职称信息。
2继承中的super
/**
* Created by apple on 20/5/1.
是当前对象里面父类对象的引用
*/
public class TestInherit {
public static void main(String[] args) {
ChildClass child = new ChildClass();
child.f();
}
}
class FatherClass{
public int value;
public void f(){
value = 100;
System.out.println("FatherClass的value" + value);
}
}
class ChildClass extends FatherClass{
public int value;
public void f(){
super.f();
value = 200;
System.out.println("ChildClass的value" + value);
System.out.println(value);
System.out.println(super.value);
}
}
输出:
FatherClass的value100
ChildClass的value200
200
100
继承
/**
* Created by apple on 20/5/1.
*/
class Person{
private String name;
private int 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;
}
}
class Student extends Person{
private String school;
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
public class TestPerson {
public static void main(String[] args) {
Student student = new Student();
student.setName("John");
student.setAge(18);
student.setSchool("清华");
System.out.println(student.getName() + "今年" + student.getAge()+",在" + student.getSchool() +"读书");
}
}
输出:
John今年18,在清华读书
3重写
/**
* Created by apple on 20/5/1.
*/
public class TestOverWrite {
public static void main(String[] args) {
Studentlei stu = new Studentlei();
stu.setAge(18);
stu.setName("Jok");
stu.setSchool("清华");
System.out.println(stu.getInfo());
}
}
class Personlei{
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;
}
private String name;
private int age;
public String getInfo(){
return name + age;
}
}
class Studentlei extends Personlei{
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
private String school;
public String getInfo(){
return getName() + getAge() + school;
}
}
输出:Jok18清华
4、基础
import com.sun.corba.se.impl.oa.poa.POAPolicyMediatorImpl_NR_UDS;
/**
* Created by apple on 20/4/11.
*/
class Point {
//定义一个点类来表示三维空间中的点,有三个坐标,要求如下
//可以生成具有特定坐标的点对象
// 提供可以设置三个坐标的方法
//提供可以计算该点到某点距离平方的方法
double x;
double y;
double z;
Point(double _x, double _y, double _z){
x = _x;
y = _y;
z = _z;
}
void setX(double _x){
x = _x;
}
void setY(double _y){
y = _y;
}
void setZ(double _z){
z = _z;
}
double getDistance(Point p){
return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) + (z - p.z) * (z - p.z);
}
}
public class TestPoint {
public static void main(String[] args) {
Point p = new Point(1.0 , 2.0, 3.0);
Point p1 = new Point(0.0, 0.0, 0.0);
System.out.println(p.getDistance(p1));
p.setX(5.0);
System.out.println(p.getDistance(new Point(1.0, 1.0, 1.0)));
}
}
5、while
/**
* Created by apple on 20/4/6.
*/
public class TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 10){
System.out.print(i);
i++;
}
System.out.println("");
i = 0;
do {
System.out.print(i);
i++;
}while (i < 10);
System.out.println("");
int stop = 5;
for ( i = 0; i <10 ; i++) {
System.out.print(i);
if(i == stop) break;
}
System.out.println("");
int skip = 4;
for (i = 0; i <7 ; i++) {
if (i == skip) continue;
System.out.print(i);
}
}
}
输出:
0123456789
0123456789
012345
012356
equals
/**
* Created by apple on 20/5/1.
*/
public class TestEquals {
public static void main(String[] args) {
Cat c1 = new Cat(1,2);
Cat c2 = new Cat(1,2);
System.out.println(c1 == c2);
System.out.println(c1.equals(c2));
String s1 = new String("he");
String s2 = new String("he");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
class Cat{
int color;
int weight;
public Cat(int color,int weight){
this.color = color;
this.weight = weight;
}
public boolean equals(Object obj){ //object类里面重写equals方法
if(obj == null) return false;
else {
if (obj instanceof Cat){
Cat c = (Cat)obj; //强制转换,把obj强制转换成猫的类型
if (c.color == this.color && c.weight == this.weight) { // 等于当前的this.color
return true;}
}
}
return false;
}
}
输出:
false
true
false
true
/**
* Created by apple on 20/5/1.
*/
public class TestEquals {
public static void main(String[] args) {
Cat c1 = new Cat(1,2);
Cat c2 = new Cat(1,2);
System.out.println(c1 == c2);
//System.out.println(c1.equals(c2));
String s1 = new String("he");
String s2 = new String("he");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
class Cat{
int color;
int weight;
public Cat(int color,int weight){
this.color = color;
this.weight = weight;
}
/* public boolean equals(Object obj){ //object类里面重写equals方法
if(obj == null) return false;
else {
if (obj instanceof Cat){
Cat c = (Cat)obj;
if (c.color == this.color && c.weight == this.weight) {
return true;}
}
}
return false;
}*/
}
false
false
true
对象转换
/**
* Created by apple on 20/5/1.
*/
public class TestAnimal {
public static void main(String[] args) {
Animal a = new Animal("name");
Cats c = new Cats("catname" ,"blue");
Dog d = new Dog("dogname", "red");
System.out.println(a instanceof Animal); //a是一只动物吗
System.out.println(c instanceof Animal); //c是一只动物吗
System.out.println(d instanceof Animal); // d是一只动物吗
System.out.println(a instanceof Cats); //a是一只猫吗
a = new Cats("big","yellow"); //父类animal的引用,指向new出的一只cat。看到的只是作为父类的看到的那些属性和方法,所以只看到名字,看不到furcolor.
System.out.println(a.name); //true
// System.out.println(a.furColor); //false
System.out.println(a instanceof Animal); //a是一只animal
System.out.println(a instanceof Cats); // a 是一只猫吗
Cats c1 = (Cats)a; //a强制转换成Cats
System.out.println(c1.furColor);
}
}
class Animal{
public String name;
Animal (String name){
this.name = name;
}
}
class Dog extends Animal{
public String eyesColor;
Dog(String n,String c){
super(n);
eyesColor = c;
}
}
class Cats extends Animal{
public String furColor;
Cats(String n,String c){
super(n);
furColor = c;
}
}
输出:
true
true
true
false
big
true
true
yellow
多态
/**
* Created by apple on 20/5/1.
*/
public class TestDuotai {
public static void main(String[] args) {
Cats2 c = new Cats2("catname","blue");
Dogs d = new Dogs("dogname","yellow");
Lady l1 = new Lady("l1",c);
Lady l2 = new Lady("l2",d);
l1.myPetEnjoy();
l2.myPetEnjoy();
}
}
class Animals {
public String name;
Animals(String name){
this.name = name;
}
public void enjoy() {
System.out.println("叫声");
}
}
class Dogs extends Animals{
private String furColor;
Dogs(String n,String c){
super(n); furColor = c;
}
public void enjoy(){
System.out.println("狗叫声");
}
}
class Cats2 extends Animals{
private String eyesColor;
Cats2(String n,String c){
super(n); eyesColor = c;
}
public void enjoy(){
System.out.println("猫叫声");
}
}
class Lady{
private String name;
private Animals pet;
//养了一只宠物,定义animal类型,用animal可以提供最大的灵活性,可以养任何animal
Lady(String n,Animals pet){
this.name = name;
this.pet = pet;
}
public void myPetEnjoy(){pet.enjoy();}
// 自己这只宠物的enjoy方法
}
输出:
猫叫声
狗叫声
抽象abstract
/**
* Created by apple on 20/5/1.
*/
public class TestDuotai {
public static void main(String[] args) {
Cats2 c = new Cats2("catname","blue");
Dogs d = new Dogs("dogname","yellow");
Lady l1 = new Lady("l1",c);
Lady l2 = new Lady("l2",d);
l1.myPetEnjoy();
l2.myPetEnjoy();
}
}
abstract class Animals {
public String name;
Animals(String name){
this.name = name;
}
/* public void enjoy() {
System.out.println("叫声");
}*/
public abstract void enjoy(); //抽象方法,
}
class Dogs extends Animals{
private String furColor;
Dogs(String n,String c){
super(n); furColor = c;
}
public void enjoy(){
System.out.println("狗叫声");
}
}
class Cats2 extends Animals{
private String eyesColor;
Cats2(String n,String c){
super(n); eyesColor = c;
}
public void enjoy(){
System.out.println("猫叫声");
}
}
class Lady{
private String name;
private Animals pet;
Lady(String n,Animals pet){
this.name = name;
this.pet = pet;
}
public void myPetEnjoy(){pet.enjoy();}
}
接口
/**
* Created by apple on 20/5/2.
*/
public interface Valuable {
public double getMoney(); //返回值money
}
interface Protectable {
public void beProtected(); //保护的
}
interface A extends Protectable{ //这个接口中有俩个方法,接口之间可相互继承。类只能实现接口
void m();
}
abstract class Anim { //抽象类
private String name;
abstract void enjoy();
}
class GoldenMonkey extends Anim implements Valuable,Protectable{ //多继承接口
public double getMoney(){
return 10000;
}
public void beProtected() {
System.out.println("live in the room");
}
public void enjoy() {
}
public void test() {
Valuable v = new GoldenMonkey(); //new出的是金丝猴,里面有很多方法,用到就一个方法getMoney
v.getMoney();
Protectable p = (Protectable)v; //把v强制转换成P,看到的只有beProtected方法
p.beProtected();
}
}
class Men implements A{
public void m() {} //定义A的m方法,和本来的beProtected方法
public void beProtected() {}
}
选择排序
/**
* Created by apple on 20/5/2.
*/
public class Numsort {
public static void main(String[] args) {
/* int[] a = new int[args.length];
for (int i = 0; i <args.length ; i++) {
a[i] = Integer.parseInt(args[i]);
}*/
int []a = {3,5,7,9,0,1};
print(a);
sortNum(a);
System.out.println("");
print(a);
}
/* private static void sortNum(int[] a){
for (int i = 0; i <a.length; i++) {
for (int j = i+1; j <a.length ; j++) {
if(a[j]<a[i]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}*/
private static void sortNum(int[] a){
int k,temp;
for (int i = 0; i <a.length; i++) {
k = i;
for (int j = k+1; j <a.length ; j++) {
if (a[j] < a[k]){
k = j;
}
}
if(k != i){
temp = a[i];
a[i] = a[k];
a[k] =temp;
}
}
}
private static void print(int[] a){
for (int i = 0; i <a.length ; i++) {
System.out.print(a[i] + " ");
}
}
}
输出:
3 5 7 9 0 1
0 1 3 5 7 9
冒泡排序
//import java.util.Date;
/**
* Created by apple on 20/5/2.
*/
public class BubbleSortDate {
public static void main(String[] args) {
Date[] days = new Date[5];
days[0] = new Date(2006,5,4);
days[1] = new Date(2005,5,4);
days[2] = new Date(2008,5,4);
days[3] = new Date(2004,6,4);
days[4] = new Date(2004,5,4);
bubbleSort(days);
for (int i = 0; i <days.length ; i++) {
System.out.println(days[i].toSring());
}
}
public static Date[] bubbleSort(Date[] a){
int len = a.length;
for (int i = len - 1; i >= 1 ; i--) {
for (int j = 0; j <= i - 1 ; j++) {
if (a[j].compare(a[j+1]) > 0){
Date temp =a[j] ;
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
}
class Date{
int year, month, day;
Date(int y ,int m, int d){
year = y;
month = m;
day = d;
}
public int compare(Date date){
return year > date.year ? 1
:year < date.year ? -1
:month > date.month ? 1
:month < date.month ? -1
:day > date.day ? 1
:day < date.day ? -1
:0;
}
public String toSring(){ //不然输出的是hashcode
return year + " " + month + " " + day;
}
}
String的split 方法和valueOf方法
/**
* Created by apple on 20/5/2.
*/
public class StringD1 {
public static void main(String[] args) {
int j = 1234098;
String num = String.valueOf(j);
System.out.println("j 是" + num.length() + "位数");
String s = "mary,F,2010";
String[] sPlit = s.split(",");
for (int i = 0; i <sPlit.length ; i++) {
System.out.println(sPlit[i]);
}
}
}
输出:
j 是7位数
mary
F
2010
charAt
/**
* Created by apple on 20/5/2.
*/
public class StringD2 {
public static void main(String[] args) {
String s = "ghjklsadfghAFGHJKSFHJ234567_-_R35-83HGHJKhj";
int lCount = 0, uCount = 0 , aCount = 0;
/* for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'z'){
lCount++;
}else if( c >= 'A' && c <= 'Z'){
uCount++;
}else {
aCount++;
}
}*/
String l = "abcdefghijklmnopqrstuvwxyz";
String u = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (l.indexOf(c) != -1){ // 第一次出现的时候的下标,若没有出现,则是-1
lCount++;
}else if( u.indexOf(c) != -1){
uCount++;
}else {
aCount++;
}
}
System.out.println(lCount + " " + uCount + " " + aCount);
}
}
一个字符串在另一个字符串中出现的次数
/**
* Created by apple on 20/5/2.
* 从一个字符串中截出某一个字符串,并计算出现的次数
*/
public class StringD3 {
public static void main(String[] args) {
String s ="ghjjavahkdfajavaghjjavaghadjava";
String sTofin = "java";
int count = 0;
int index = -1;
while ((index = s.indexOf(sTofin)) != -1){
s = s.substring(index + sTofin.length());
count++;
System.out.println(index); //在一个串里,另一个字符串出现的位置
}
System.out.println(count);
}
}
3
5
3
4
4
StringBuffer
/**
* Created by apple on 20/5/2.
* StirngBuffer 可变的字符序列
*/
public class StringD4 {
public static void main(String[] args) {
String s = "meinv";
char[] a = {'s','d','f'};
StringBuffer sb1 = new StringBuffer((s));
sb1.append('/').append("xiaoqin").append("dudu");
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("数字");
for (int i = 0; i < 9; i++) {
sb2.append(i);
}
System.out.println(sb2);
sb2.delete(8,sb2.length()).insert(0,a);
System.out.println(sb2);
sb2.reverse();
System.out.println(sb2);
}
}
输出
meinv/xiaoqindudu
数字012345678
sdf数字012345
543210字数fds
基础数据类型包装
/**
* Created by apple on 20/5/2.
*/
public class StringD5 {
public static void main(String[] args) {
Integer i = new Integer(100);
Double d = new Double("123.456");
int j = i.intValue() + d.intValue();
float f = i.floatValue() + d.floatValue();
System.out.println(j);
System.out.println(f);
double pi = Double.parseDouble("3.114159");
double r = Double.valueOf("2.0").doubleValue();
double s = pi*r*r;
System.out.println(s);
try {
int k = Integer.parseInt("1.78");
}catch (NumberFormatException e){
System.out.println("格式不对");
}
System.out.println(Integer.toBinaryString(123) + "B");
System.out.println(Integer.toHexString(123) + "H");
}
}
输出
223
223.456
12.456636
格式不对
1111011B
7bH
切割
/**
* Created by apple on 20/5/2.
*/
public class ArrayParser {
public static void main(String[] args) {
String s = "1,2;3,45;6;7,8";
String[] sFirst = s.split(";");
for (int i = 0; i < sFirst.length; i++) {
System.out.println(sFirst[i]);
String[] sSecond = sFirst[i].split(",");
for (int j = 0; j < sSecond.length; j++) {
// System.out.println(sSecond[j]);
}
}
}
}
先按;切割开来, 再按,切割开来
File
import javax.imageio.IIOException;
import java.io.File;
import java.io.IOException;
/**
* Created by apple on 20/5/2.
*/
public class TestFile {
public static void main(String[] args) {
String separator = File.separator;//路径的一种写法,自动是/,文件的分隔符
String filename = "my.txt";
String directory = "mydir1" + separator + "mydir2"; //定义了一个路径
File f = new File(directory,filename); //z只是在内存里的一个对象
if (f.exists()){
System.out.println("文件名" + f.getAbsolutePath());
System.out.println("文件大小" + f.length());
}else {
f.getParentFile().mkdirs(); //求他的父路径,建立一系列路径 mkdirs
try {
f.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
输出: 文件名/Users/apple/Documents/gittest/Test1/mydir1/mydir2/my.txt
文件大小0
以树状结构展示特定的文件夹及其子文件夹
/**
* Created by apple on 20/5/2.
* 创建文件的目录,父类格式,用递归的方法,找出其返回值,参数。找到父类文件,用listFiles求他的孩子,然后递归方法找孩子的孩子
*/
import java.io.*;
public class Filelist {
public static void main(String[] args) {
File f =new File("/Users/apple/Documents/gittest/Test1/src");
System.out.println(f.getName());
tree(f,1);
}
//定义一个方法,列出当前目录下的孩子
private static void tree(File f, int level){ //level代表现在几层了,,根据层次定义往里缩进多少
String preStr = " ";
for (int i = 0; i < level; i++) {
preStr += " "; //leve是几,往里缩进多少个空格
}
File[] childs = f.listFiles();
for (int i = 0; i < childs.length ; i++) {
System.out.println(preStr + childs[i].getName());
//找孩子的孩子
if (childs[i].isDirectory()){ //如果他的孩子是个路径,继续找其孩子
tree(childs[i], level + 1);
}
}
}
}
Enum
/**
* Created by apple on 20/5/3.
*/
public class EnumD {
public enum myColor {red,blue,yellow};
public static void main(String[] args) {
myColor m = myColor.blue;
switch (m){
case red:
System.out.println("是红色");
break;
case blue:
System.out.println("是蓝色");
break;
case yellow:
System.out.println("是黄色");
break;
default:
System.out.println("都不是");
}
System.out.println(m);
}
}
输出
是蓝色
blue
菲薄列函数
package review;
/**
* Created by apple on 20/5/5.
*/
public class Feibilie {
public static void main(String[] args) {
System.out.println(f(7));
}
public static int f(int n){
if (n ==1 || n == 2 ){
return 1;
}else
return f(n-1) + f(n-2);
}
}
网友评论