- 😄 Graduate student.
- 💻 I focus on Remote sensing change detection and Video action recognition.
- ☕ Ping-pong,badminton,music,sci-fi movies.
wanghao15536870732 / studynotes Goto Github PK
View Code? Open in Web Editor NEW📖 学习笔记
📖 学习笔记
import java.io.File;
interface USB{
public void start();
public void stop();
}
class Computer{
public void plugin(USB usb){
usb.start();
usb.stop();
}
}
class Flash implements USB{
public void start(){
System.out.println("U盘开始使用");
}
public void stop(){
System.out.println("U盘停止使用");
}
}
class Print implements USB{
public void start(){
System.out.println("打印机开始工作");
}
public void stop() {
System.out.println("打印机停止使用");
}
}
interface Fruit{ //定义接口
public void eat();
}
class Apple implements Fruit{ //定义接口子类
public void eat(){
System.out.println("*** 吃苹果。");
}
}
class Orange implements Fruit{
public void eat(){
System.out.println("*** 吃橘子。");
}
}
//工厂设计模式
class Factory{ //定义工厂类,此类不属于属性
/*
* 取得指定类型的接口对象
* @param className 要取得的类实例化对象标记
* @return 如果指定标记存在,则Fruite接口实例化对象,否则返回 null
*/
public static Fruit getInstance(String className){
if("apple".equals(className)){ //判断是否是苹果类
return new Apple();
}else if("orange".equals(className)){
return new Orange();
}else{
return null;
}
}
}
public class TestDemo5{
public static void main(String[] args) {
Computer com = new Computer();
com.plugin(new Flash());
com.plugin(new Print());
Fruit f = new Apple(); //子类实例化父类对象
f.eat(); //调用被覆写的方法
System.out.println("-----------------------");
Fruit f1 = Factory.getInstance("orange"); //通过工厂类取得指定标记的对象
f1.eat(); //调用接口方法
Fruit f2 = Factory.getInstance("apple");
f2.eat();
}
}
String的引用传递:
class Message{
private int num = 10;
/*
* 本类没有提供无参构造方法,而是提供有参构造,可以接收num属性的内容
* @author haowang
* */
public Message(int num){
this.num = num;
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
}
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title = title;
this.price = price;
}
public void apple(){
System.out.println("wanghao");
}
public void setPrice(double price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public String getTitle() {
return title;
}
public String getInfo(){
this.apple();//调用本类的方法,只能放在构造方法的首行,并且一定要保留调用的出口
return "书名:" + title + ",价格:" + price;
}
}
class Member{
private int mid;
private String name;
private Car car; //表示属于人的车,如果没有车,则内容为null
public Member(int mid,String name){
this.mid = mid;
this.name = name;
}
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return this.car;
}
public String getInfo(){
return "人的编号:" + this.mid + ",姓名:" + this.name;
}
}
class Car{
private Member member; //车属于一个人,如果没有所属者,则为null
private String pname; //车的名字
public Car(String pname){
this.pname = pname;
}
public void setMember(Member member) {
this.member = member;
}
public Member getMember() {
return this.member;
}
public String getInfo(){
return "车的名字:" + this.pname;
}
}
public class Demo {
public static void main(String[] args) {
Book book = new Book("java开发",89.2);
System.out.println(book.getInfo());
Message msg = new Message(30); //实例化Message类对象同时传递num属性内容
fun(msg); //引用传递
System.out.println(msg.getNum());
Member m = new Member(1,"wanghao "); //独立对象
Car c = new Car("八手奥拓100"); //独立对象
m.setCar(c); //一个人有一辆车
c.setMember(m); //一辆车属于一个人
System.out.println(m.getCar().getInfo()); //通过人找到车的信息
System.out.println(c.getMember().getInfo()); //通过车找到人的信息
}
public static void fun(Message temp){
temp.setNum(100); //修改num属性内容
}
}
结果:
wanghao
书名:java开发,价格:89.2
100
车的名字:八手奥拓100
人的编号:1,姓名:wanghao
import com.sun.istack.internal.localization.NullLocalizable;
class Node{
private String data;
private Node next;
public Node(String data){ //必须有数据才有Node
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
public class TestDemo4 {
public static void main(String[] args) {
Node root = new Node("火车头");
Node n1 = new Node("车厢A");
Node n2 = new Node("车厢B");
Node n3 = new Node("车厢C");
root.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
Node p = root;
while(p != null)
{
System.out.println(p.getData());
p = p.getNext();
}
System.out.println("-----------------");
print(root);
}
public static void print(Node p){
if(p == null){
return;
}
System.out.println(p.getData());
print(p.getNext()); //递归操作
}
}
package com.company.team.androidlab;
public class OutterClass {
private static double PI = 3.14;
private double r = 10;
static class SInner {
static void getPerimeter() {
//静态内部类的方法只能访问外部类的类成员变量和方法
System.out.println(2 * PI * 10);
}
void getArea() {
System.out.println(PI * 10 * 10);
}
}
class OInner {
void getPerimeter() {
//对象内部类的方法可以直接访问外部类的成员变量和方法
System.out.println(2 * PI * r);
}
}
public static void main(String[] args) {
//访问静态内部类的静态方法
OutterClass.SInner.getPerimeter();
//访问静态内部类的非静态方法
OutterClass.SInner os = new OutterClass.SInner();
os.getArea();
//访问对象内部类的方法
//没有找出来错误在哪
// OutterClass.OInner oc = new OutterClass.OInner();
oc.getPerimeter();
}
}
class Book2{
private String title;
private double price;
public Book2(String t,double p){
title = t;
price = p;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public String getInfo(){
return "书名:" + title + ",价格:" + price;
}
}
public class ArrayDemo {
public static void main(String[] args) {
Book2 books[] = new Book2[3]; //开辟了一个3个长度的对象数组,内容为NULL
books[0] = new Book2("java讲义",34.5);
books[1] = new Book2("Androd讲义",99.9);
books[2] = new Book2("疯狂java讲义",199);
for (int i = 0; i < books.length; i++) {
System.out.println(books[i].getInfo());
}
int date[] = new int[]{4,7,5,3,1,0,16,1,9,8};
// int temp[] = date; //数组引用传递
java.util.Arrays.sort(date); //直接引用java.util.Arrays.sort(数组);来进行排序
//等同于用冒泡排序执行
// for (int i = 1; i <= date.length - 1; i++) {
// for (int j = 1; j <= date.length - i; j++) {
// if (date[j - 1] > date[j]) {
// int t = date[j - 1];
// date[j - 1] = date[j];
// date[j] = t;
// }
// }
// }
//或者用选择排序
// for (int i = 1; i <= date.length - 1; i++) {
// int pos = 0;
// for (int j = 1; j <= date.length - i; j++) {
// if(date[pos] < date[j]){
// pos = j;
// }
// }
// if (pos != date.length - i){
// int temp = date[pos];
// date[pos] = date[date.length - i];
// date[date.length - i] = temp;
// }
// }
for (int i = 0; i < date.length; i++) {
System.out.print(date[i] + "->");
}
// for (int i = 0; i < temp.length; i++) {
// System.out.print(temp[i] + "--");
// }
}
}
结果:
书名:java讲义,价格:34.5
书名:Androd讲义,价格:99.9
书名:疯狂java讲义,价格:199.0
0->1->1->3->4->5->7->8->9->16->
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title = title;
this.price = price;
}
//对象比较实现
public boolean compare(Book book) {
if (book == null) {
return false;
}
//执行b4.compare(b5)代码时会有两个对象
// 当前对象this(调用方法对象,就是b4引用)
//传递的对象book(引用传递,就是b5引用)
if (this == book){ //内存地址比较
return true;
}
if (this.title.equals(book.title)
&& this.price == price){ //属性判断
return true;
}else {
return false;
}
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
class Info{
private String msg = "Hello!";
public void print(){
System.out.println("msg = " + msg);
}
public void fun(Info temp){ //本类接受本类对象
temp.msg = "wnaghao"; //在累的内部直接利用对象访问私有属性
}
}
public class TestDemo {
public static void main(String[] args) {
Book b1 = new Book("java开发",79.8);
Book b2 = new Book("Android开发",99.9);
if (b1.getTitle().equals(b2.getTitle())
&& b1.getPrice() == b2.getPrice()){ //属性比较不知道为啥不能两个都是equals
System.out.println("是同一个对象!");
}else{
System.out.println("不是同一个对象!");
}
Info In = new Info();
In.fun(In); //没有意义,只是一个语法验证
In.print();
Book b4 = new Book("java开发",78.9);
Book b5 = new Book("java开发",78.9);
if (b4.compare(b5)) {
System.out.println("是同一个对象!");
}else {
System.out.println("不是同一个对象!");
}
}
}
//严格意义上来讲,String是一个类。
//所以equals()完成的就是对象比较操作,
// 具备null的验证,为null时直接返回false
interface USB{
public void start();
public void end();
}
class Computer{
public void plugin(USB usb){
usb.start();
usb.end();
}
}
class Flash implements USB{
public void start(){
System.out.println("U盘开始工作");
}
public void end(){
System.out.println("U盘停止工作");
}
}
class Print implements USB{
public void start(){
System.out.println("打印机开始工作");
}
public void end(){
System.out.println("打印机停止工作");
}
}
interface Fruit{
public void eat(); //定义抽象方法
}
class Apple implements Fruit{
public void eat(){
System.out.println("***吃苹果");
}
}
class Orange implements Fruit{
public void eat(){
System.out.println("***吃橘子");
}
}
class Factory{ //工厂模式
/*
*取得指定类型的接口对象
*@param className 要取得的类实例化对象标记
*@return 如果指定标记存在,则fruit接口的实例化对象,否则返回null
*/
public static Fruit getInstance(String className){
if("apple".equals(className)){
return new Apple();
}else if("orange".equals(className)){
return new Orange();
}else{
return null;
}
}
}
interface Network{
public void browse(); //定义浏览的抽象方法
}
class Real implements Network{
public void browse(){
System.out.println("上网浏览信息");
}
}
//代理上网
class Proxy implements Network{ private Network network;
public Proxy(Network network){ //设置代理的真实操作
this.network = network; //设置代理的子类
}
//与具体上网相关的操作
public void click(){
System.out.println("检查用户是否合法");
}
public void browse(){
this.click(); //可以调用多个与具体业务相关的操作
this.network.browse(); //调用真实上网操作
}
}
public class TestDemo{
public static void main(String args[]){
System.out.println("Hello World");
Computer com = new Computer();
com.plugin(new Flash());
com.plugin(new Print());
Fruit f = new Apple(); //子类实例化父类对象
f.eat(); //调用被复写的方法
Fruit o = new Orange();
o.eat();
Fruit f1 = Factory.getInstance("orange");
f1.eat();
Fruit f2 = Factory.getInstance("apple");
f2.eat();
Network net = null;
//实例化代理,同时传入代理的真实操作
net = new Proxy(new Real());
//客户只关心上网浏览一个功能
net.browse();
}
}
/*所谓代理模式就是指一个代理主题来代替主题来操作真是主题,真实主题执行主题的业务操作
*而代理主题负责其他相关业务的处理。就好比现实中经常使用到的代理上网,客户经过网络代理
*链接网络,由代理服务器完成用户权限、等与上网操作相关的操作
*不管是代理操作也好,真是的操作也好,都是为了上网,所以用户关心的只是如何上网,至于里
*如何操作的,用户并不关心
byte b[] = {97, 98, 99, 100, 101, 102, 103, 104};
String str1 = new String(b, 3, 3);
String str2 = new String(b, 1, 4);
String str3 = new String(b);
System.out.println(str1.concat(str2));
StringBuffer sb = new StringBuffer(str1);
sb.append(str2);
System.out.println(sb.insert(1,'r'));
System.out.println(sb.reverse());
#include"stdio.h"
#include"stdlib.h"
#define M 20 /*预定义图的最大顶点数*/
#include"string.h"
typedef struct node{ //边表结点
int adjvex; //邻接点
struct node *next;
}edgenode;
typedef struct vnode{ //头节点类型
char vertex; //顶点信息
edgenode *firstedge; //邻接表表头指针
}vertexnode;
typedef struct{ //邻接表类型
vertexnode adjlist[M]; //存放头节点的顺序表
int n,e; //图的顶点数与边数
}linkedgraph;
int visited[M];
void createwithfile(linkedgraph *g,char *filename,int c) //c = 0表示创建无向图
{
int i,j,k;
edgenode *s;
FILE *fp;
fp = fopen(filename,"r");
if(fp)
{
fscanf(fp,"%d%d",&g->n,&g->e); //读入顶点数与边数
for(i = 0;i < g->n;i ++)
{
fscanf(fp,"%d",&g->adjlist[i].vertex); //读入顶点信息
g->adjlist[i].firstedge = NULL; //边表置为空表
}
for(k =0 ;k < g->e;k ++) //循环e次建立边表
{
fscanf(fp,"%d%d",&i,&j); //读入无序对(i,j)
s = (edgenode*)malloc(sizeof(edgenode));
s->adjvex = j; //邻接点序号为j
s->next = g->adjlist[i].firstedge;
g->adjlist[i].firstedge = s; //将新节点*s插入顶点Vi的边表头部
if(c == 0)
{
s = (edgenode*)malloc(sizeof(edgenode));
s->adjvex = i; //邻接点序号为i
s->next = g->adjlist[j].firstedge; //将新节点*s插入顶点Vj的边表头部
g->adjlist[j].firstedge = s;
}
}
fclose(fp);
}
else
g->n = 0; ///文件打开失败
}
linkedgraph *create(int c) //c = 0表示创建无向图
{
linkedgraph *g;
g = (linkedgraph *)malloc(sizeof(linkedgraph));
int i,j,k;
edgenode *s;
printf("输入结点数和边数:\n");
scanf("%d%d",&g->n,&g->e); //读入顶点数与边数
printf("输入结点值:\n");
getchar();
for(i = 0;i < g->n;i ++)
{
scanf("%c",&g->adjlist[i].vertex); //读入顶点信息
getchar();
g->adjlist[i].firstedge = NULL; //边表置为空表
}
if(c == 0)
printf("输入无序对:\n");
else
printf("输入有序对:\n");
for(k = 0 ;k < g->e;k ++) //循环e次建立边表
{
scanf("%d%d",&i,&j); //读入无序对(i,j)
s = (edgenode *)malloc(sizeof(edgenode));
s->adjvex = j; //邻接点序号为j
s->next = g->adjlist[i].firstedge;
g->adjlist[i].firstedge = s; //将新节点*s插入顶点Vi的边表头部
if(c == 0)
{
s = (edgenode*)malloc(sizeof(edgenode));
s->adjvex = i; //邻接点序号为i
s->next = g->adjlist[j].firstedge; //将新节点*s插入顶点Vj的边表头部
g->adjlist[j].firstedge = s;
}
}
return g;
}
void dfs(linkedgraph *g,int i)
{
edgenode *p;
printf("visit vertex:%c \n",g->adjlist[i].vertex); //访问顶点i
visited[i] = 1;
p = g->adjlist[i].firstedge;
while(p) //从p的邻接点出发进行深度优先搜索
{
if(!visited[p->adjvex])
dfs(g,p->adjvex);
p = p->next;
}
}
void dfstraverse(linkedgraph *g)
{
int i;
for(i = 0;i < g->n;i ++)
visited[i] = 0;
for(i = 0;i < g->n;i ++)
if(!visited[i])
dfs(g,i);
}
void bfs(linkedgraph *g,int i)
{
int j;
edgenode *p;
int queue[M],front,rear; //创建一个队列
front = rear = 0; //初始化空队列
printf("%c ",g->adjlist[i].vertex); //访问根节点
visited[i] = 1;
queue[rear ++] = i; //被访问结点进队
while(rear > front)
{
j = queue[front ++]; //出队
p = g->adjlist[j].firstedge;
while(p)
{
if(visited[p->adjvex] == 0)
{
printf("%c ",g->adjlist[p->adjvex].vertex);
queue[rear ++] = p->adjvex;
visited[p->adjvex] = 1;
}
p = p->next;
}
}
}
int bfstraverse(linkedgraph *g)
{
int i,count = 0;
for(i = 0;i < g->n;i ++)
visited[i] = 0;
for(i = 0;i < g->n;i ++)
if(!visited[i]) //vi未被访问过
{
printf("\n");
count ++;
bfs(g,i);
}
return count;
}
int main()
{
linkedgraph *h;
h = create(0);
printf("深度遍历:\n");
dfstraverse(h);
printf("广度遍历:\n");
int count = bfstraverse(h);
printf("\n此图的连通分量为:\n");
printf("%d\n",count);
return 0;
}
对象的构造方法:
package com.company.Test;
class Book{
private String title;
private double price;
//在一个类里对构造方法重载时,参数由多到少
public Book(){
System.out.println("无参调用");
}
public Book(String S){
setTitle(S);
System.out.println("有一个参数的构造");
}
public Book(String t,double p){ //定义构造方法
// setTitle(t); //调用本类方法
// setPrice(p); //调用本类方法
title = t; //也可以直接为属性赋值
price = p; //也可以直接为属性赋值
System.out.println("有两个参数的构造");
}
//setter和getter方法
public void setPrice(double price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public String getTitle() {
return title;
}
public void getInfo(){
System.out.println("图书名称:" + title + ",输的价格:" + price);
}
}
public class TestDemo {
public static void main(String[] args) {
Book book1 = new Book(); //调用无参数的方法
book1.getInfo();
Book book = new Book("java开发"); //调用一个参数的方法
book.getInfo();
Book book2 = new Book("java开发",88.8);//调用两个参数的方法
book2.getInfo();
book2.getInfo();
new Book("Android开发",69.8).getInfo(); //匿名对象的构建
}
}
结果:
无参调用
图书名称:null,输的价格:0.0
有一个参数的构造
图书名称:java开发,输的价格:0.0
有两个参数的构造
图书名称:java开发,输的价格:88.8
有两个参数的构造
图书名称:Android开发,输的价格:69.8
Cast(造型) :
package com.company.team.androidlab;
public class Animals {
void breathe(){
System.out.println("动物呼吸........");
}
final static void live(Animals an){
an.breathe();
}
}
class Fish extends Animals{
void swim(){
System.out.println("I can swimiing.....");
}
void breathe(){
System.out.println("鱼儿breathe....");
}
}
public class CastUpdownTest {
public static void main(String[] args) {
//fish 的上溯为Animal,将丢失在父类Animal上增加的方法
Animals an = new Fish();
an.breathe();
//因此下面调用swim就会出现错误
// an.swim();
//覆盖弗雷德方法不会损失掉
Fish f = (Fish) an;
f.swim();
f.breathe();
}
}
过多造成难以辨别 :
//经过多次上溯造型和下溯造型,当不清楚对象是否为某个类的对象时可以用运算符 instanceof 来判断
// object instanceof class
// object intanceof interface
interface Pinterface{
//.........
}
class PCclass{
//.........
}
class Sub extends PCclass implements Pinterface{
//............
}
public class InstanceofTest{
public static void main(String[] args) {
PCclass s = new Sub();
Pinterface pi = new Sub();
System.out.println(s instanceof Sub);
System.out.println(s instanceof PCclass);
System.out.println(s instanceof Pinterface);
System.out.println(s instanceof Object);
}
}
class Node{
private String data;
private Node next;
public Node(String data){ //必须有数据才有Node
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
class Node2{
private String data;
private Node2 next;
public Node2(String data){
this.data = data;
}
public String getData() {
return this.data;
}
public Node2 getNext() {
return this.next;
}
/*
* 实现节点的增加(递归调用,目的是将新结点保存到最后一个结点之后)
* 第一次调用(Link):this = Link.root
* 第二次调用(Link):this = Link.root.next
* 第三次调用(Link):this = Link.root.next.next
* @param newNode2 新结点,结点抽象由Link类创建
* */
public void addNode(Node2 newNode2){
if(this.next == null){ //当前结点的下一个为null
this.next = newNode2; //保存新结点
}else { //当前结点之后还存在结点
this.next.addNode(newNode2); //当前结点的下一个节点继续保存
}
}/*
* 递归的方式输出每个结点保存的数据
* 第一次调用(Link):this = Link.root
* 第二次调用(Link):this = Link.root.next
* 第三次调用(Link):this = Link.root.next.next
* @param newNode2 新结点,结点抽象由Link类创建
* */
public void printNode2(){
System.out.println(this.data); //输出当前结点数据
if(this.next != null){ //判断下一个节点
this.next.printNode2(); //找到下一个节点递归输出
}
}
}
class Link{ //负责数据的设置和输出
private Node2 root; //根节点
/*
* 向链表中增加新的数据,如果当前链表没有结点则将下一个数据作为结点
* 如果有节点则使用Node类将新结点保存到最后一个结点之后
* @param data 要保存的数据*/
public void add(String data){
Node2 newNode2 = new Node2(data); //设置数据的先后关系所以将data包装在一个Node类对象中
if (this.root == null){ //一个链表只有一个根节点
this.root = newNode2; //将新的结点设置为根节点
}else { //根节点已将存在
this.root.addNode(newNode2); // 交由Node类来进行结点保存
}
}
public void print(){ //输出数据
if (this.root != null){ //存在根节点
this.root.printNode2(); //交给Node类进行输出
}
}
}
public class TestDemo4 {
public static void main(String[] args) {
Node root = new Node("火车头");
Node n1 = new Node("车厢A");
Node n2 = new Node("车厢B");
Node n3 = new Node("车厢C");
root.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
Node p = root;
while(p != null)
{
System.out.println(p.getData());
p = p.getNext();
}
System.out.println("-----------------");
print(root);
System.out.println("-----------------------");
Link link = new Link();
link.add("中北大学");
link.add("大数据需学院");
link.add("七班");
link.add("1707004716王浩");
link.print();
}
public static void print(Node p){
if(p == null){
return;
}
System.out.println(p.getData());
print(p.getNext()); //递归操作
}
}
结果 :
火车头
车厢A
车厢B
车厢C
-----------------
火车头
车厢A
车厢B
车厢C
-----------------------
中北大学
大数据需学院
七班
1707004716王浩
public class ShiftTest {
public static void main(String[] args){
int a = 0b1001_1101; //十进制157
int b = 0b0001_1101; //十进制57
System.out.println(a);
System.out.println(a << 3); //左移三位(带符号)
System.out.println(a >> 3); //右移三位 (带符号)
System.out.println(a >>> 3); //右移三位(不带符号)
System.out.println(~a); //按位取反
System.out.println(a & b); //按位或
System.out.println(a | b); //按位与
System.out.println(a ^ b); //按位异或
System.out.println(Integer.toBinaryString(-13));
System.out.println(6 ^ 5);
int grade;
grade = 10;
if (grade >= 60) //条件表达式的值应为布尔类型
System.out.println("passed");
else
System.out.println("failed");
String []sa = {"何以" + "解忧","唯有Java!"};
for (int i = 0; i < sa.length; i++) {
System.out.println(sa[i]);
}
System.err.println("!!!!!!!!!!!!!!"); //输出的信息为红色
System.out.printf("n1 = %.2f\tn2 = %6.2f\n",100,200);
System.out.printf("E = % 2$ .2f\tPI = % 1$ 6.4f\n", Math.PI, Math.E);
System.out.printf(" % 2$ d\t% 1$ d\n",100,200);
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
结果
!!!!!!!!!!!!!!(红色的感叹号)
157
1256
19
19
-158
29
157
128
11111111111111111111111111110011
3
failed
何以解忧
唯有Java!
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
<&: |: ^: 7 ^ 4 :
6 & 3 = 2 ; 6 | 5 = 7 ; 6 ^ 5 = 3; 1 1 1
1 1 0 1 1 0 1 1 0 ^ 1 0 0
& 0 1 0 | 1 0 1 ^ 1 0 1 = 0 1 1
= 0 1 0 = 2 = 1 1 1 = 7 = 0 1 1 = 3 ^1 0 0
= 1 1 1 = 7; 即7 ^ 4 ^ 4 = 7;
//一个数 异或同一个数两次,结果还是那个数>
<< : 其实就是乘以2的移动的次数次幂
>> : 就是除以2移动的位数次幂
3 << 2 = 12; 3 << 1 = 6; 3 << 3 = 24
3 * 4 = 12; 3 * 2 = 6; 3 * 8 = 24;
00 || 0000-0000 0000-0000 0000-0000 0000-0011 || 00
3 << 2 ----> 3 * 2 * 2
>> : 最高位补什么有原有数据的最高位置决定,
如果最高位0,右移后,用0补空位。
如果最高位1,右移后,用1补空位。
<< : 无论最高位是什么,右移后,都用0补。
package com.company;
public class Person {
public int legs;
public String kind;
public void Animal() {
setLegs(4);
}
public void Animal(int l) {
setLegs(l);
}
public void eat() {
System.out.println("Eating");
}
public void move() {
System.out.println("Moving");
}
public void setLegs(int l) {
if (l != 0 && l != 2 && l != 4) {
System.out.println("Wrong number of legs!");
return;
}
legs = l;
}
public int getLegs() {
return legs;
}
public void setKind(String str) {
kind = str;
}
public String getKind() {
return kind;
}
}
public class Bird extends Person{
public Bird(){
setLegs(4);
setKind("Bird");
}
public void move()
{
System.out.println("Flying");
}
}
public class Ostrich extends Bird{
public Ostrich(){
setLegs(2);
setKind("Ostrich");
}
public void move(){
System.out.println("Running!");
}
public void hideHead(){
System.out.println("Hidding the head...");
}
}
public class Zoo{
public static void main(String[] args){
Person person = new Ostrich();
person.move();
}
}
#include <stdlib.h>
#include <string.h>
#define m 20
#define maxsize 100
typedef struct
{
int key;
int card;
char name[m];
int num;
char department[m];
}recordtype;
typedef struct
{
recordtype r[maxsize+1];
int length;
}node;
typedef struct
{
int data[maxsize];
int len;
}seqlist;
typedef struct
{
int key;
int address;
}indexnode;
node *create()
{
node *p;
int i;
p=(node *)malloc(sizeof(node));
printf ("输入学生个数:\n");
scanf ("%d",&p->length);
getchar();
/*p->r[0].card=0;
p->r[0].name[m]='\0';
p->r[0].num=0;
p->r[0].department[m]='\0';*/
printf ("输入学生的卡号,姓名,班号,系别:\n");
for (i=0;i<p->length;i++)
{
scanf ("%d%s%d%s",&p->r[i].card,&p->r[i].name,&p->r[i].num,&p->r[i].department);
}
return p;
}
void quicksort(node *p,int left,int right)
{
int i,j;
if (left<right)
{
i=left,j=right;
p->r[0].card=p->r[i].card;
strcpy(p->r[0].name,p->r[i].name);
p->r[0].num=p->r[i].num;
strcpy(p->r[0].department,p->r[i].department);
do
{
while (p->r[j].card>p->r[0].card&&i<j)
j--;
if (i<j)
{
p->r[i].card=p->r[j].card;
strcpy(p->r[i].name,p->r[j].name);
p->r[i].num=p->r[j].num;
strcpy(p->r[i].department,p->r[j].department);
i++;
}
while (p->r[i].card<p->r[0].card&&i<j)
i++;
if (i<j)
{
p->r[j].card=p->r[i].card;
strcpy(p->r[j].name,p->r[i].name);
p->r[j].num=p->r[i].num;
strcpy(p->r[j].department,p->r[i].department);
j--;
}
}while (i!=j);
p->r[i].card=p->r[0].card;
strcpy(p->r[i].name,p->r[0].name);
p->r[i].num=p->r[0].num;
strcpy(p->r[i].department,p->r[0].department);
quicksort(p,left,i-1);
quicksort(p,i+1,right);
}
}
void display(node *p)
{
int i;
for (i=1;i<=p->length;i++)
{
printf ("%d ",p->r[i].card);
printf ("%s ",p->r[i].name);
printf ("%d ",p->r[i].num);
printf ("%s\n",p->r[i].department);
}
}
void search(node *p,char n[m])
{
int i;
for (i=0;i<p->length;i++)
if (strcmp(p->r[i].department,n)==0)
printf ("%d %s %d %s\n",p->r[i].card,p->r[i].name,p->r[i].num,p->r[i].department);
}
int main()
{
node *p,*q;
int j,n;
char i[m];
p=(node *)malloc(sizeof(node));
q=(node *)malloc(sizeof(node));
printf ("----学生信息----\n");
p=create();
printf ("输入左右边界的下标值:\n");
scanf ("%d%d",&n,&j);
quicksort(p,n,j);
printf ("输出排序后的结果:\n");
display(p);
printf ("输入要查找的系别:\n");
scanf ("%s",&i);
getchar();
printf ("输出%s系别对应的学生信息:\n",i);
search(p,i);
return 0;
}```
![cq k3ezp48xmql0v9k n](https://user-images.githubusercontent.com/39999166/49655187-f2b68b80-fa74-11e8-9fa8-bec77f135d9b.png)
String类的基本概念:
public class StringDemo{
public static void main(String[] args) {
String str = new String("www.Test.com");
System.out.println(str);
String sta = "Hello";
String stb = new String("Hello"); //stb使用关键字new开辟了新的堆内空间
String stc = stb;
System.out.println(sta == stb); //false
System.out.println(sta == stc); // false //String 比较时比较的只是内存地址的数值,而不是内容
System.out.println(stb == stc); // true 因为stb和stc指向的是同一个对内存中的"Hello",因此输出为true
//使用equals进行的是字符串内容的比较
System.out.println(sta.equals(stb)); //true
System.out.println(sta.equals(stc)); //true
System.out.println(stb.equals(stc)); //true
String stra = "Hello";
String strb = "Hello";
String strc = "Hello";
System.out.println(stra == strb);//每一个字符串都是一个String的匿名对象
String str2 = new String("Hello"); //所以会首先在堆内存中开辟一块空间炮村字符串"Hello"
// 然后使用关键字new,开辟另一块对内存空间,这种构造方法
System.out.println(stra == strc); //true
System.out.println(strb == strc); //true
String strd = new String("Hello");//此时不会自动保存到对象池中
String stre = "Hello"; //此时将开辟新的空间
System.out.println(strd == stre);// /结果为false
String strd2 = new String("Hello").intern();//使用构造方法定义新的内存空间,手工入池
String stre2 = "Hello";
System.out.println(strd2 == stre2); //结果为true
}
}
String的部分方法:
char c = str.charAt(0); //截取第一个字符
System.out.println(c);
char []date = str.toCharArray(); //将字符串变为字符数组
for (int i = 0; i < date.length; i++) {
System.out.print(date[i] + "->"); // h->e->l->l->o->
}
System.out.println();
for (int i = 0; i < date.length; i++) {
date[i] -= 32;
}
System.out.println(new String(date)); //将全部字符数组变为String HELLO
System.out.println(new String(date,1,2)); //将部分字符数组变为String EL
byte[]date1 = str.getBytes(); //将字符串变为字节数组
for (int i = 0; i < date1.length; i++) {
date1[i] -= 32;
}
System.out.println(new String(date1)); //HELLO
System.out.println(new String(date1,2,2));//LL
//字符串的比较
String strf = "Hello";
System.out.println(str.equals(strf)); //false 区分大小写
System.out.println(str.equalsIgnoreCase(strf)); //true 不区分大小写
System.out.println(str.compareTo(strf)); // 32 比较的是两字符串的编码差值
//字符串的查找
System.out.println(str.indexOf("e")); // 1 返回的是满足条件单词的第一个字母的索引
System.out.println(str.lastIndexOf("e"));// 3
public void setstudentcourse(String[] courses){
class course{
private String[] courses;
private int coursenum;
public course(String[] course){
courses = course;
coursenum = course.length;
getinfo();
}
private void get() {
for (int i = 0; i < coursenum; i++) {
System.out.print(" " + courses[i]);
}
}
private void getinfo() {
get();
}
}
new course(courses);
}
public static void main(String[] args){
Person st = new Person();
String[] courses = {"语文","数学","英语","化学"};
st.setstudentcourse(courses);
}
}
str.substring(i);// 提取字符串的第i + 1 个元素开始到结束位置的字符串
System.out.println(str1.concat(str2));//使用concat方法将两个字符串str1,str2连到一起
byte b[] = {97,98,99,100,101,102,103,104};
String str1 = new String(b,3,3);//输出b数组从位置3往后3个长度的字符串
equal//判断两个字符串是否相等
equalsIgnoreCase(String str)//此方法忽略字符串大小写
StringBuffer sb = new StringBuffer(str1);
sb.append(str2);//将str2连接到str1后
System.out.println(sb.insert(1,'r'));//在1位置插入字符'r';
public class Person {
public static void main(String[] args){
pro pro = new pro();
pro.print();
pril p = new pril();
p.print(2);
System.out.println(2 * (p.x));
System.out.println(Math.floor(4.5));
System.out.println(Math.round(4.5));
}
}
class pro
{
public void print()
{
for (int i = 0; i < 100; i++) {
if ((i % 3) == 0 && (i % 5) == 0 && (i % 9) == 0 )
System.out.println(i);
}
}
}
class pril
{
public int x = 1;
public void print(int y)
{
System.out.println(2 * y);
}
}
//引用传递的应用
package com.company.Test.This;
class Message{
private int num = 10;
/*
* 本类没有提供无参构造方法,而是提供有参构造,可以接收num属性的内容
* @author haowang
* */
public Message(int num){
this.num = num;
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
}
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title = title;
this.price = price;
}
public void apple(){
System.out.println("wanghao");
}
public void setPrice(double price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public String getTitle() {
return title;
}
public String getInfo(){
this.apple();//调用本类的方法,只能放在构造方法的首行,并且一定要保留调用的出口
return "书名:" + title + ",价格:" + price;
}
}
class Member{
private int mid;
private String name;
private Car car;
public Member(int mid,String name){
this.mid = mid;
this.name = name;
}
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return this.car;
}
public String getInfo(){
return "人的编号:" + this.mid + ",姓名:" + this.name;
}
}
class Car{
private Member member;
private String pname;
public Car(String pname){
this.pname = pname;
}
public void setMember(Member member) {
this.member = member;
}
public Member getMember() {
return this.member;
}
public String getInfo(){
return "车的名字:" + this.pname;
}
}
public class Demo {
public static void main(String[] args) {
Book book = new Book("java开发",89.2);
System.out.println(book.getInfo());
Message msg = new Message(30); //实例化Message类对象同时传递num属性内容
fun(msg); //引用传递
System.out.println(msg.getNum());
Member m = new Member(1,"wanghao "); //独立对象
Car c = new Car("八手奥拓100"); /独立对象
m.setCar(c); //一个人有一辆车
c.setMember(m); //一辆车属于一个人
System.out.println(m.getCar().getInfo()); //通过人找到车的信息
System.out.println(c.getMember().getInfo()); //通过车找到人的信息
}
public static void fun(Message temp){
temp.setNum(100); //修改num属性内容
}
}
//数据表与简单的java映射
package com.company.Test.This;
import com.sun.istack.internal.localization.NullLocalizable;
class Dept{
private int deotno; //部门编号
private String dname; //部门名称
private String loc; //部门位置
private Emp emps[]; //多个雇员
public Dept(int deotno,String dname,String loc) {
this.deotno = deotno;
this.dname = dname;
this.loc = loc;
}
// setter,getter,无参函数构造
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public Emp[] getEmps() {
return emps;
}
public void setDeotno(int deotno) {
this.deotno = deotno;
}
public void setDname(String dname) {
this.dname = dname;
}
public void setLoc(String loc) {
this.loc = loc;
}
public int getDeotno() {
return deotno;
}
public String getDname() {
return dname;
}
public String getLoc() {
return loc;
}
public String getInfo(){
return "部门编号:" + this.deotno + ",名称:" + this.dname + ",位置:" + this.loc;
}
}
class Emp{
private int empon; //雇员编号
private String ename; //雇员姓名
private String job; //雇员职位
private double sal; //基本工资
private double comm; //佣金
private Dept dept;
private Emp mgr; //表示雇员对应的领导
public Emp(int empon,String ename,String job,double sal,double comm){
this.empon = empon;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
// setter,getter,无参函数构造
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEmpon(int empon) {
this.empon = empon;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setJob(String job) {
this.job = job;
}
public void setSal(double sal) {
this.sal = sal;
}
public void setComm(double comm) {
this.comm = comm;
}
public Dept getDept() {
return dept;
}
public int getEmpon() {
return empon;
}
public String getEname() {
return ename;
}
public String getJob() {
return job;
}
public double getSal() {
return sal;
}
public double getComm() {
return comm;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Emp getMgr() {
return mgr;
}
public String getInfo() {
return "雇员编号:" + this.empon + ",姓名:" + this.ename + ",职位:" + this.job +
",工资:" + this.sal + ",佣金:" + this.comm;
}
}
public class TestDemo1 {
public static void main(String[] args) {
//1.产生各自的独立对象
Dept dept = new Dept(10,"实践部暨心理站","中北大学"); //部门信息
Emp ea = new Emp(1707004716,"王浩","干事",3000,0.0); //雇员信息
Emp eb = new Emp(1707005717,"徐刘杰","副部",4000,0.0); //雇员信息
Emp ec = new Emp(1707006718,"肖文博","部长",4500,0.0); //雇员信息
//2.设置雇员跟领导的关系
ea.setMgr(eb);
eb.setMgr(ec);
//设置雇员跟部门的关系
ea.setDept(dept); //雇员与部门
eb.setDept(dept); //雇员与部门
ec.setDept(dept); //雇员与部门
dept.setEmps(new Emp[]{ea,eb,ec}); //部门与雇员
System.out.println(ea.getInfo());
System.out.println(ea.getMgr().getInfo());
System.out.println(ea.getDept().getInfo());
System.out.println("-------------------------------");
System.out.println(dept.getInfo());
for (int i = 0; i < dept.getEmps().length; i++) {
System.out.println(dept.getEmps()[i].getInfo());
if(dept.getEmps()[i].getMgr() != null) //判断是否有领导
System.out.println("\t\t" + dept.getEmps()[i].getMgr().getInfo());
}
}
}
private class course {
private String[] courses;
private int coursenum;
// 内部类的构造器
public course(String[] course) {
courses = course;
coursenum = course.length;
getinfo(); //调用方法 getinfo()
}
private void get() {
for (int i = 0; i < coursenum; i++) {
System.out.print(" " + courses[i]);
}
}
private void getinfo() {
get(); 调用方法 get()
}
}
public static void main(String[] args){
st.setstudentcourse(courses);//访问内部类
}
public class ShiftTest {
public static void main(String[] args){
int a = 0b1001_1101; //十进制157
int b = 0b0001_1101; //十进制57
System.out.println(a);
System.out.println(a << 3); //左移三位(带符号)
System.out.println(a >> 3); //右移三位 (带符号)
System.out.println(a >>> 3); //右移三位(不带符号)
System.out.println(~a); //按位取反
System.out.println(a & b); //按位或
System.out.println(a | b); //按位与
System.out.println(a ^ b); //按位异或
System.out.println(Integer.toBinaryString(-13));
System.out.println(6 ^ 5);
int grade;
grade = 10;
if (grade >= 60) //条件表达式的值应为布尔类型
System.out.println("passed");
else
System.out.println("failed");
String []sa = {"何以" + "解忧","唯有Java!"};
for (int i = 0; i < sa.length; i++) {
System.out.println(sa[i]);
}
System.err.println("!!!!!!!!!!!!!!"); //输出的信息为红色
// System.out.printf("n1 = %.2f\tn2 = %6.2f\n",100,200);
// System.out.printf("E = % 2$ .2f\tPI = % 1$ 6.4f\n", Math.PI, Math.E);
// System.out.printf(" % 2$ d\t% 1$ d\n",100,200);
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
结果
!!!!!!!!!!!!!!(红色的感叹号)
157
1256
19
19
-158
29
157
128
11111111111111111111111111110011
3
failed
何以解忧
唯有Java!
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
<&: |: ^: 7 ^ 4 :
6 & 3 = 2 ; 6 | 5 = 7 ; 6 ^ 5 = 3; 1 1 1
1 1 0 1 1 0 1 1 0 ^ 1 0 0
& 0 1 0 | 1 0 1 ^ 1 0 1 = 0 1 1
= 0 1 0 = 2 = 1 1 1 = 7 = 0 1 1 = 3 ^1 0 0
= 1 1 1 = 7; 即7 ^ 4 ^ 4 = 7;
//一个数 异或同一个数两次,结果还是那个数>
<< : 其实就是乘以2的移动的次数次幂
>> : 就是除以2移动的位数次幂
3 << 2 = 12; 3 << 1 = 6; 3 << 3 = 24
3 * 4 = 12; 3 * 2 = 6; 3 * 8 = 24;
00 || 0000-0000 0000-0000 0000-0000 0000-0011 || 00
3 << 2 ----> 3 * 2 * 2
>> : 最高位补什么有原有数据的最高位置决定,
如果最高位0,右移后,用0补空位。
如果最高位1,右移后,用1补空位。
<< : 无论最高位是什么,右移后,都用0补。
public class ShiftTest {
public static void main(String[] args){
int a = 0b1001_1101; //十进制157
int b = 0b0001_1101; //十进制57
System.out.println(a);
System.out.println(a << 3); //左移三位(带符号)
System.out.println(a >> 3); //右移三位 (带符号)
System.out.println(a >>> 3); //右移三位(不带符号)
System.out.println(~a); //按位取反
System.out.println(a & b); //按位或
System.out.println(a | b); //按位与
System.out.println(a ^ b); //按位异或
System.out.println(Integer.toBinaryString(-13));
System.out.println(6 ^ 5);
int grade;
grade = 10;
if (grade >= 60) //条件表达式的值应为布尔类型
System.out.println("passed");
else
System.out.println("failed");
String []sa = {"何以" + "解忧","唯有Java!"};
for (int i = 0; i < sa.length; i++) {
System.out.println(sa[i]);
}
System.err.println("!!!!!!!!!!!!!!"); //输出的信息为红色
// System.out.printf("n1 = %.2f\tn2 = %6.2f\n",100,200);
// System.out.printf("E = % 2$ .2f\tPI = % 1$ 6.4f\n", Math.PI, Math.E);
// System.out.printf(" % 2$ d\t% 1$ d\n",100,200);
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
结果
!!!!!!!!!!!!!!(红色的感叹号)
157
1256
19
19
-158
29
157
128
11111111111111111111111111110011
3
failed
何以解忧
唯有Java!
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
<&: |: ^: 7 ^ 4 :
6 & 3 = 2 ; 6 | 5 = 7 ; 6 ^ 5 = 3; 1 1 1
1 1 0 1 1 0 1 1 0 ^ 1 0 0
& 0 1 0 | 1 0 1 ^ 1 0 1 = 0 1 1
= 0 1 0 = 2 = 1 1 1 = 7 = 0 1 1 = 3 ^1 0 0
= 1 1 1 = 7; 即7 ^ 4 ^ 4 = 7;
//一个数 异或同一个数两次,结果还是那个数>
<< : 其实就是乘以2的移动的次数次幂
: 就是除以2移动的位数次幂
3 << 2 = 12; 3 << 1 = 6; 3 << 3 = 24
3 * 4 = 12; 3 * 2 = 6; 3 * 8 = 24;
00 || 0000-0000 0000-0000 0000-0000 0000-0011 || 00
3 << 2 ----> 3 * 2 * 2
: 最高位补什么有原有数据的最高位置决定,
如果最高位0,右移后,用0补空位。
如果最高位1,右移后,用1补空位。
<< : 无论最高位是什么,右移后,都用0补。
package com.company;
class Book2{
private String title;
private double price;
static String pub = "清华大学出版社";
public Book2(String title,double price){
this.title = title;
this.price = price;
}
public String getInfo(){
return "书的名称:" + this.title + ",书的价格:" + this.price + ",出版社:"+ this.pub;
}
}
class Book{
String title;
double price;
public void getInfp(){
System.out.println("书的名称:" + title + ",书的价格:" + price);
}
}
public class Main {
public static void main(String[] args) {
Book bk = new Book(); //声明并实例化第一个对象
Book bk2 = new Book(); //声明并实例化第二个对象
bk.title = "java开发"; //设置第一个对象的属性内容
bk.price = 89.90; //设置第一个对象的属性内容
bk2.title = "第一行代码,";//设置第二个对象的属性内容
bk2.price = 90.00; //设置第二个对象的属性内容
bk.getInfp(); //调用方法
bk2.getInfp();
Book2 ba = new Book2("java开发",10.2); //实例化Book2类对象
Book2 bb = new Book2("Android开发",11.2); //实例化Book2类对象
Book2 bc = new Book2("Oracle开发",12.2); //实例化Book2类对象
ba.pub = "北京大学出版社"; //修改了一个属性的内容
System.out.println(ba.getInfo());
System.out.println(bb.getInfo());
System.out.println(bc.getInfo());
System.out.println(Book2.pub);
Book2.pub = "中北大学出版社"; //再没有实例化对象的情况下直接输出属性内容
System.out.println(ba.getInfo()); //修改static 属性内容
System.out.println(bb.getInfo()); //实例化Book类对象
System.out.println(bc.getInfo()); //输出Book2类对象信息
}
}
class Book2{
private String title;
private double price;
static String pub = "清华大学出版社";
public Book2(String title,double price){
this.title = title;
this.price = price;
}
public String getInfo(){
return "书的名称:" + this.title + ",书的价格:" + this.price + ",出版社:"+ this.pub;
}
}
class Book{
String title;
double price;
public void getInfp(){
System.out.println("书的名称:" + title + ",书的价格:" + price);
}
}
public class Main {
public static void main(String[] args) {
Book bk = new Book(); //声明并实例化第一个对象
Book bk2 = new Book(); //声明并实例化第二个对象
bk.title = "java开发"; //设置第一个对象的属性内容
bk.price = 89.90; //设置第一个对象的属性内容
bk2.title = "第一行代码,";//设置第二个对象的属性内容
bk2.price = 90.00; //设置第二个对象的属性内容
bk.getInfp(); //调用方法
bk2.getInfp();
Book2 ba = new Book2("java开发",10.2); //实例化Book2类对象
Book2 bb = new Book2("Android开发",11.2); //实例化Book2类对象
Book2 bc = new Book2("Oracle开发",12.2); //实例化Book2类对象
ba.pub = "北京大学出版社"; //修改了一个属性的内容
System.out.println(ba.getInfo());
System.out.println(bb.getInfo());
System.out.println(bc.getInfo());
System.out.println(Book2.pub);
Book2.pub = "中北大学出版社"; //再没有实例化对象的情况下直接输出属性内容
System.out.println(ba.getInfo()); //修改static 属性内容
System.out.println(bb.getInfo()); //实例化Book类对象
System.out.println(bc.getInfo()); //输出Book2类对象信息
}
}
class Book{
private String title;
private double price;
public void setTitle(String title) {
this.title = title;
}
public void setPrice(double price) {
if(price > 0){
this.price = price;
}
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public void getInfo(){
System.out.println("图书名称:" + title + ",输的价格:" + price);
}
}
public class Demo {
public static void main(String[] args) {
Book book = new Book();
book.setTitle("java开发");
book.setPrice(-89.9);
book.getInfo();
}
}
结果:图书名称:java开发,输的价格:0.0
class Creature{
public Creature(){
System.out.println("Creature的无参构造器");
}
}
class Animal extends Creature{
public Animal(String name){
System.out.println("Animal带有一个参数的构造器" + "我的名字" + name);
}
public Animal(String name,int age){
this(name);
System.out.println("Animal带有两个参数的构造器" + "我的年龄" + age);
}
}
class Parent {
public String tag = "疯狂的丁逸群";
}
class Derived extends Parent{
private String tag = "轻量级的杨旭";
}
class BaseClass{
public int a = 5;
}
public class Wolf extends Animal{
public Wolf(){
super("莉莉安",18);
System.out.println("WOlf的无参函数构造器");
}
public static void main(String[] args) {
new Wolf();
}
}
public class SubClass extends BaseClass{
public int a= 7;
public void accessOwner(){
System.out.println(a);
}
public void accessBase(){
System.out.println(super.a);
}
public static void main(String[] args) {
SubClass sc = new SubClass();
sc.accessOwner();
sc.accessBase();
}
}
public class HideTest{
public static void main(String[] args) {
Derived d = new Derived();
//程序不可访问d的私有变量tag,所以下面的语句将引起编译错误
// System.out.println(d.tag);
System.out.println(((Parent)d).tag);
//直接调用Parent的tag成员变量
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.