当您编辑另一个类的对象的变量的值时,该类是更改还是仅更改该对象

时间:2014-08-26 23:53:19

标签: java class oop object

可能是一个愚蠢的问题,我完全不知道如何用我想要的方式来说出来。但是,这是我的问题。我写了一个程序,它完全符合我的要求。这是1700行代码,我没有使用任何面向对象的编程方法。所以我想我自己想在OOP标准中完全改写它。我做了什么。但是,它运行不一样。

所以我想我的问题是。如果我有5个班级和1个班级。这些都是从每个变量继承某些变量,并在main中创建它们的对象。现在当这些变量被程序方法的正常运行改变时。似乎一切都没有改变。那是因为这些类不断发回相同的信息而没有被改变吗?即使我正在更改其特定对象中的值。

我真的不能说出我想要的问题,因为我不是专家,这是我第一次真正尝试完全功能性的OOP计划。所以请尊重我在这里寻求建议,而不是侮辱。

编辑 - 我将提供我的所有代码,以避免更多的混淆。我得到的大部分内容应该足以让我有更好的想法,但是其他一些我无法适应的歌曲类别.nbd

主:

 import java.awt.*;
 import javax.swing.*;
 import java.util.*;
 import java.awt.image.*;
 import java.net.*;
 import java.awt.event.*;
 import java.io.*;
 import javax.sound.sampled.*;



 public class KeyBoardHero extends JFrame implements Runnable, KeyListener,           MouseListener, MouseMotionListener
 {
Thread gameloop;

int clickX, clickY;

UserInterface ui = new UserInterface();
Logic logic1 = new Logic();
CrazyTrain crazyTrain1 = new CrazyTrain();
BroHymn broHymn1 = new BroHymn();
Sandman sandman1 = new Sandman();


public static void main(String[] args)
{
    new KeyBoardHero();
}


public KeyBoardHero()
{
    super("KeyBoard Hero");
    setSize(750,530);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ui.loadGraphics();

    crazyTrain1.loadAudio();
    broHymn1.loadAudio();
    sandman1.loadAudio();

    gameloop = new Thread(this);
    gameloop.start();

    addKeyListener(this);
    addMouseListener(this);
    addMouseMotionListener(this);
}


public void run()
{
    Thread t = Thread.currentThread();
    while (t == gameloop)
    {
        try
        {
            Thread.sleep(5);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        gameUpdate();
    }
}


public void gameUpdate()
{
    ui.loadGraphics2();
    repaint();
}


public void paint(Graphics g)
{
    //backbuffer
    g.drawImage(ui.backbuffer, 0, 0, this);
}


public void keyPressed(KeyEvent e)
{
    switch (e.getKeyCode())
    {
        //F1***************
        case KeyEvent.VK_F1:

        crazyTrain1.f1();
        sandman1.f1();

        break;

        //F2***************
        case KeyEvent.VK_F2:

        crazyTrain1.f2();
        sandman1.f2();

        break;

        //F3***************
        case KeyEvent.VK_F3:

        crazyTrain1.f3();
        sandman1.f3();

        break;

        //F4***************
        case KeyEvent.VK_F4:

        crazyTrain1.f4();
        broHymn1.f4();
        sandman1.f4();

        break;

        //F5***************
        case KeyEvent.VK_F5:

        crazyTrain1.f5();
        broHymn1.f5();
        sandman1.f5();

        break;

        //F6***************
        case KeyEvent.VK_F6:

        crazyTrain1.f6();
        broHymn1.f6();
        sandman1.f6();

        break;

        //F7***************
        case KeyEvent.VK_F7:

        crazyTrain1.f7();
        broHymn1.f7();

        break;

        //F8***************
        case KeyEvent.VK_F8:

        crazyTrain1.f8();
        broHymn1.f8();
        sandman1.f8();

        break;
    }
}


public void keyReleased(KeyEvent e)
{
    switch (e.getKeyCode())
    {
        //F1****release****
        case KeyEvent.VK_F1:

        logic1.release1();

            break;

        //F1****release****
        case KeyEvent.VK_F2:

        logic1.release2();

            break;

        //F1****release****
        case KeyEvent.VK_F3:

        logic1.release3();

            break;

        //F1****release****
        case KeyEvent.VK_F4:

        logic1.release4();

            break;

        //F1****release****
        case KeyEvent.VK_F5:

        logic1.release5();

            break;

        //F1****release****
        case KeyEvent.VK_F6:

        logic1.release6();

            break;

        //F1****release****
        case KeyEvent.VK_F7:

        logic1.release7();

            break;

        //F1****release****
        case KeyEvent.VK_F8:

        logic1.release8();

            break;
    }
}


public void keyTyped(KeyEvent e) {}

public void mouseClicked(MouseEvent e)
{
    clickX = e.getX();
    clickY = e.getY();

    logic1.mouseClick();
}


public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e)
{
    clickX = e.getX();
    clickY = e.getY();

    logic1.mouseRelease();
}


public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
    clickX = e.getX();
    clickY = e.getY();

    logic1.mouseMove();
}

}

逻辑类:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;



public class Logic extends UserInterface
{
int crazyCounter = 0;
int broCounter = 0;
int sandmanCounter = 0;

//personal method I made to reset all red keys off screen at once
public void removeRed()
{
    red1Pos.x = -8888;
    red1Pos.y = -8888;
    red2Pos.x = -8888;
    red2Pos.y = -8888;
    red3Pos.x = -8888;
    red3Pos.y = -8888;
    red4Pos.x = -8888;
    red4Pos.y = -8888;
    red5Pos.x = -8888;
    red5Pos.y = -8888;
    red6Pos.x = -8888;
    red6Pos.y = -8888;
    red7Pos.x = -8888;
    red7Pos.y = -8888;
    red8Pos.x = -8888;
    red8Pos.y = -8888;
}

//personal method to replace all keys at once if I need to
public void placeAll()
{
    fKeyOne.x = 370;
    fKeyOne.y = 195;
    fKeyTwo.x = 420;
    fKeyTwo.y = 195;
    fKeyThree.x = 470;
    fKeyThree.y = 195;
    fKeyFour.x = 520;
    fKeyFour.y = 195;
    fKeyFive.x = 370;
    fKeyFive.y = 245;
    fKeySix.x = 420;
    fKeySix.y = 245;
    fKeySeven.x = 470;
    fKeySeven.y = 245;
    fKeyEight.x = 520;
    fKeyEight.y = 245;
}


//personal methods to remove a certain key from screen or add
public void removeOne()
{
    fKeyOne.x = -8888;
    fKeyOne.y = -8888;
}

public void removeTwo()
{
    fKeyTwo.x = -8888;
    fKeyTwo.y = -8888;
}

public void removeThree()
{
    fKeyThree.x = -8888;
    fKeyThree.y = -8888;
}

public void removeFour()
{
    fKeyFour.x = -8888;
    fKeyFour.y = -8888;
}

public void removeFive()
{
    fKeyFive.x = -8888;
    fKeyFive.y = -8888;
}

public void removeSix()
{
    fKeySix.x = -8888;
    fKeySix.y = -8888;
}

public void removeSeven()
{
    fKeySeven.x = -8888;
    fKeySeven.y = -8888;
}

public void removeEight()
{
    fKeyEight.x = -8888;
    fKeyEight.y = -8888;
}

public void placeOne()
{
    fKeyOne.x = 370;
    fKeyOne.y = 195;
}

public void placeTwo()
{
    fKeyTwo.x = 420;
    fKeyTwo.y = 195;
}

public void placeThree()
{
    fKeyThree.x = 470;
    fKeyThree.y = 195;
}

public void placeFour()
{
    fKeyFour.x = 520;
    fKeyFour.y = 195;
}

public void placeFive()
{
    fKeyFive.x = 370;
    fKeyFive.y = 245;
}

public void placeSix()
{
    fKeySix.x = 420;
    fKeySix.y = 245;
}

public void placeSeven()
{
    fKeySeven.x = 470;
    fKeySeven.y = 245;
}

public void placeEight()
{
    fKeyEight.x = 520;
    fKeyEight.y = 245;
}

public void placeRedOne()
{
    red1Pos.x = 370;
    red1Pos.y = 195;
}

public void placeRedTwo()
{
    red2Pos.x = 420;
    red2Pos.y = 195;
}

public void placeRedThree()
{
    red3Pos.x = 470;
    red3Pos.y = 195;
}

public void placeRedFour()
{
    red4Pos.x = 520;
    red4Pos.y = 195;
}

public void placeRedFive()
{
    red5Pos.x = 370;
    red5Pos.y = 245;
}

public void placeRedSix()
{
    red6Pos.x = 420;
    red6Pos.y = 245;
}

public void placeRedSeven()
{
    red7Pos.x = 470;
    red7Pos.y = 245;
}

public void placeRedEight()
{
    red8Pos.x = 520;
    red8Pos.y = 245;
}

public void release1()
{
    fKeyOne.x = 370;
    fKeyOne.y = 195;
    down1Off.x = -8888;
    down1Off.y = -8888;
}

public void release2()
{
    fKeyTwo.x = 420;
    fKeyTwo.y = 195;
    down2Off.x = -8888;
    down2Off.y = -8888;
}

public void release3()
{
    fKeyThree.x = 470;
    fKeyThree.y = 195;
    down3Off.x = -8888;
    down3Off.y = -8888;
}

public void release4()
{
    fKeyFour.x = 520;
    fKeyFour.y = 195;
    down4Off.x = -8888;
    down4Off.y = -8888;

    if(broCounter > 11 && broCounter < 25 && songArrowPos.y == broArrow.y)
    {
        removeFour();
    }

    if(broCounter >= 32 && broCounter <= 34 && songArrowPos.y == broArrow.y)
    {
        removeFour();
    }
}

public void release5()
{
    fKeyFive.x = 370;
    fKeyFive.y = 245;
    down5Off.x = -8888;
    down5Off.y = -8888;

    if((broCounter <= 8) && (songArrowPos.y == broArrow.y))
    {
        removeFive();
    }
}

public void release6()
{
    fKeySix.x = 420;
    fKeySix.y = 245;
    down6Off.x = -8888;
    down6Off.y = -8888;
}

public void release7()
{
    fKeySeven.x = 470;
    fKeySeven.y = 245;
    down7Off.x = -8888;
    down7Off.y = -8888;

    if(broCounter >= 25 && broCounter < 31 && songArrowPos.y == broArrow.y)
    {
        removeSeven();
    }
}

public void release8()
{
    fKeyEight.x = 520;
    fKeyEight.y = 245;
    down8Off.x = -8888;
    down8Off.y = -8888;
}

public void mouseClick()
{

    if((clickX > crazyArrow.x) && (clickX < crazyArrow.x + 370) && (clickY > crazyArrow.y) && (clickY < crazyArrow.y + 45))
    {
        broHover.x = -8888;
        broHover.y = -8888;
        sandHover.x = -8888;
        sandHover.y = -8888;

        removeRed();
        placeAll();
        removeTwo();
        placeRedTwo();

        crazyCounter = 0;
        songArrowPos.y = 333;
    }

    if((clickX > broArrow.x) && (clickX < broArrow.x + 420) && (clickY > broArrow.y) && (clickY < broArrow.y + 45))
    {
        crazyHover.x = -8888;
        crazyHover.y = -8888;
        sandHover.x = -8888;
        sandHover.y = -8888;

        removeRed();
        placeAll();
        removeFive();
        placeRedFive();

        broCounter = 0;
        songArrowPos.y = 389;
    }

    if((clickX > sandmanArrow.x) && (clickX < sandmanArrow.x + 470) && (clickY > sandmanArrow.y) && (clickY < sandmanArrow.y + 45))
    {
        broHover.x = -8888;
        broHover.y = -8888;
        crazyHover.x = -8888;
        crazyHover.y = -8888;

        removeRed();
        placeAll();
        removeOne();
        placeRedOne();

        sandmanCounter = 0;
        songArrowPos.y = 445;
    }
}

public void mouseRelease()
{
    if((clickX > crazyArrow.x) && (clickX < crazyArrow.x + 370) && (clickY > crazyArrow.y) && (clickY < crazyArrow.y + 45))
    {
        broHover.x = -8888;
        broHover.y = -8888;
        sandHover.x = -8888;
        sandHover.y = -8888;


        removeRed();
        placeAll();
        removeTwo();
        placeRedTwo();

        crazyCounter = 0;
        songArrowPos.y = 333;
    }

    if((clickX > broArrow.x) && (clickX < broArrow.x + 420) && (clickY > broArrow.y) && (clickY < broArrow.y + 45))
    {
        crazyHover.x = -8888;
        crazyHover.y = -8888;
        sandHover.x = -8888;
        sandHover.y = -8888;


        removeRed();
        placeAll();
        removeFive();
        placeRedFive();

        broCounter = 0;
        songArrowPos.y = 389;
    }

    if((clickX > sandmanArrow.x) && (clickX < sandmanArrow.x + 470) && (clickY > sandmanArrow.y) && (clickY < sandmanArrow.y + 45))
    {
        broHover.x = -8888;
        broHover.y = -8888;
        crazyHover.x = -8888;
        crazyHover.y = -8888;


        removeRed();
        placeAll();
        removeOne();
        placeRedOne();

        sandmanCounter = 0;
        songArrowPos.y = 445;
    }
}

public void mouseMove()
{
    if((clickX > crazyArrow.x) && (clickX < crazyArrow.x + 370) && (clickY > crazyArrow.y) && (clickY < crazyArrow.y + 45))
    {
        crazyHover.x = 320;
        crazyHover.y = 330;
    }

    if(!((clickX > crazyArrow.x) && (clickX < crazyArrow.x + 370) && (clickY > crazyArrow.y) && (clickY < crazyArrow.y + 45)))
    {
        crazyHover.x = -8888;
        crazyHover.y = -8888;
    }

    if(songArrowPos.y == 333)
    {
        crazyHover.x = 320;
        crazyHover.y = 330;
    }

    if((clickX > broArrow.x) && (clickX < broArrow.x + 420) && (clickY > broArrow.y) && (clickY < broArrow.y + 45))
    {
        broHover.x = 292;
        broHover.y = 386;
    }

    if(!((clickX > broArrow.x) && (clickX < broArrow.x + 420) && (clickY > broArrow.y) && (clickY < broArrow.y + 45)))
    {
        broHover.x = -8888;
        broHover.y = -8888;
    }

    if(songArrowPos.y == 389)
    {
        broHover.x = 292;
        broHover.y = 386;
    }

    if((clickX > sandmanArrow.x) && (clickX < sandmanArrow.x + 470) && (clickY > sandmanArrow.y) && (clickY < sandmanArrow.y + 45))
    {
        sandHover.x = 254;
        sandHover.y = 442;
    }

    if(!((clickX > sandmanArrow.x) && (clickX < sandmanArrow.x + 470) && (clickY > sandmanArrow.y) && (clickY < sandmanArrow.y + 45)))
    {
        sandHover.x = -8888;
        sandHover.y = -8888;
    }

    if(songArrowPos.y == 445)
    {
        sandHover.x = 254;
        sandHover.y = 442;
    }
}

}

UserInterface类:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;



public class UserInterface extends JFrame
{
BufferedImage backbuffer;
Graphics2D g2d;

int ScreenWidth = 750;
int ScreenHeight = 530;

int clickX, clickY;

Image background;

Image selectCrazyHover;
Image selectBroHover;
Image selectSandHover;

Image f1KeyUp;
Image f1KeyDown;
Image f2KeyUp;
Image f2KeyDown;
Image f3KeyUp;
Image f3KeyDown;
Image f4KeyUp;
Image f4KeyDown;
Image f5KeyUp;
Image f5KeyDown;
Image f6KeyUp;
Image f6KeyDown;
Image f7KeyUp;
Image f7KeyDown;
Image f8KeyUp;
Image f8KeyDown;

Image red1;
Image red2;
Image red3;
Image red4;
Image red5;
Image red6;
Image red7;
Image red8;

//used for collision detection and song rotations
Point fKeyArrowPos = new Point(363, 188);
Point songArrowPos = new Point(140, 333);

//stationary positions for collision detection of song choice
Point crazyArrow = new Point(295, 333);
Point broArrow = new Point(270, 389);
Point sandmanArrow = new Point(245, 445);

//stationary positions for the collision detection of Fkeys
Point fKeyOne = new Point (370, 195);
Point fKeyTwo = new Point (420, 195);
Point fKeyThree = new Point (470, 195);
Point fKeyFour = new Point (520, 195);
Point fKeyFive = new Point (370, 245);
Point fKeySix = new Point (420, 245);
Point fKeySeven = new Point (470, 245);
Point fKeyEight = new Point (520, 245);

//setting initial down keys off screen(invisible)
Point down1Off = new Point (-8888, -8888);
Point down2Off = new Point (-8888, -8888);
Point down3Off = new Point (-8888, -8888);
Point down4Off = new Point (-8888, -8888);
Point down5Off = new Point (-8888, -8888);
Point down6Off = new Point (-8888, -8888);
Point down7Off = new Point (-8888, -8888);
Point down8Off = new Point (-8888, -8888);

//red keys off screen(invisible)
Point red1Pos = new Point (-8888, -8888);
Point red2Pos = new Point (420, 195);
Point red3Pos = new Point (-8888, -8888);
Point red4Pos = new Point (-8888, -8888);
Point red5Pos = new Point (-8888, -8888);
Point red6Pos = new Point (-8888, -8888);
Point red7Pos = new Point (-8888, -8888);
Point red8Pos = new Point (-8888, -8888);

//song title hover effects set off screen(invisible)
Point crazyHover = new Point (-8888, -8888);
Point broHover = new Point (-8888, -8888);
Point sandHover = new Point (-8888, -8888);


public void loadGraphics()
{
    backbuffer = new BufferedImage(ScreenWidth, ScreenHeight, BufferedImage.TYPE_INT_RGB);
    g2d = backbuffer.createGraphics();

    Toolkit tk = Toolkit.getDefaultToolkit();
    background = tk.getImage(getURL("images/background.png"));
    f1KeyUp = tk.getImage(getURL("images/fkey1up.png"));
    f1KeyDown = tk.getImage(getURL("images/fkey1down.png"));
    f2KeyUp = tk.getImage(getURL("images/fkey2up.png"));
    f2KeyDown = tk.getImage(getURL("images/fkey2down.png"));
    f3KeyUp = tk.getImage(getURL("images/fkey3up.png"));
    f3KeyDown = tk.getImage(getURL("images/fkey3down.png"));
    f4KeyUp = tk.getImage(getURL("images/fkey4up.png"));
    f4KeyDown = tk.getImage(getURL("images/fkey4down.png"));
    f5KeyUp = tk.getImage(getURL("images/fkey5up.png"));
    f5KeyDown = tk.getImage(getURL("images/fkey5down.png"));
    f6KeyUp = tk.getImage(getURL("images/fkey6up.png"));
    f6KeyDown = tk.getImage(getURL("images/fkey6down.png"));
    f7KeyUp = tk.getImage(getURL("images/fkey7up.png"));
    f7KeyDown = tk.getImage(getURL("images/fkey7down.png"));
    f8KeyUp = tk.getImage(getURL("images/fkey8up.png"));
    f8KeyDown = tk.getImage(getURL("images/fkey8down.png"));

    selectCrazyHover = tk.getImage(getURL("images/crazyGlow.png"));
    selectBroHover = tk.getImage(getURL("images/broGlow.png"));
    selectSandHover = tk.getImage(getURL("images/sandGlow.png"));

    red1 = tk.getImage(getURL("images/red1.png"));
    red2 = tk.getImage(getURL("images/red2.png"));
    red3 = tk.getImage(getURL("images/red3.png"));
    red4 = tk.getImage(getURL("images/red4.png"));
    red5 = tk.getImage(getURL("images/red5.png"));
    red6 = tk.getImage(getURL("images/red6.png"));
    red7 = tk.getImage(getURL("images/red7.png"));
    red8 = tk.getImage(getURL("images/red8.png"));
}


private URL getURL(String filename)
{
    URL url = null;
    try
    {
        url = this.getClass().getResource(filename);
    }
    catch (Exception e) {}
    return url;
}

public void loadGraphics2()
{
    //background
    g2d.drawImage(background, 15, 35, this);

    //fkeys up and red keys **order determines z-index**
    g2d.drawImage(red1, red1Pos.x, red1Pos.y, this);
    g2d.drawImage(f1KeyUp, fKeyOne.x, fKeyOne.y, this);

    g2d.drawImage(f2KeyUp, fKeyTwo.x, fKeyTwo.y, this);
    g2d.drawImage(red2, red2Pos.x, red2Pos.y, this);

    g2d.drawImage(red3, red3Pos.x, red3Pos.y, this);
    g2d.drawImage(f3KeyUp, fKeyThree.x, fKeyThree.y, this);

    g2d.drawImage(red4, red4Pos.x, red4Pos.y, this);
    g2d.drawImage(f4KeyUp, fKeyFour.x, fKeyFour.y, this);

    g2d.drawImage(red5, red5Pos.x, red5Pos.y, this);
    g2d.drawImage(f5KeyUp, fKeyFive.x, fKeyFive.y, this);

    g2d.drawImage(red6, red6Pos.x, red6Pos.y, this);
    g2d.drawImage(f6KeyUp, fKeySix.x, fKeySix.y, this);

    g2d.drawImage(red7, red7Pos.x, red7Pos.y, this);
    g2d.drawImage(f7KeyUp, fKeySeven.x, fKeySeven.y, this);

    g2d.drawImage(red8, red8Pos.x, red8Pos.y, this);
    g2d.drawImage(f8KeyUp, fKeyEight.x, fKeyEight.y, this);

    //fkeys down
    g2d.drawImage(f1KeyDown, down1Off.x, down1Off.y, this);
    g2d.drawImage(f2KeyDown, down2Off.x, down2Off.y, this);
    g2d.drawImage(f3KeyDown, down3Off.x, down3Off.y, this);
    g2d.drawImage(f4KeyDown, down4Off.x, down4Off.y, this);
    g2d.drawImage(f5KeyDown, down5Off.x, down5Off.y, this);
    g2d.drawImage(f6KeyDown, down6Off.x, down6Off.y, this);
    g2d.drawImage(f7KeyDown, down7Off.x, down7Off.y, this);
    g2d.drawImage(f8KeyDown, down8Off.x, down8Off.y, this);

    //selection hovers
    g2d.drawImage(selectCrazyHover, crazyHover.x, crazyHover.y, this);
    g2d.drawImage(selectBroHover, broHover.x, broHover.y, this);
    g2d.drawImage(selectSandHover, sandHover.x, sandHover.y, this);
}

}

CrazyTrain课程:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;



public class CrazyTrain extends Logic
{
String soundFile1 = "sounds/CTE0.wav";
String soundFile2 = "sounds/CTE2.wav";
String soundFile3 = "sounds/CTE4.wav";
String soundFile4 = "sounds/CTE5.wav";
String soundFile5 = "sounds/CTA2.wav";
String soundFile6 = "sounds/CTA4.wav";
String soundFile7 = "sounds/CTA5.wav";

AudioInputStream sample1;
AudioInputStream sample2;
AudioInputStream sample3;
AudioInputStream sample4;
AudioInputStream sample5;
AudioInputStream sample6;
AudioInputStream sample7;

Clip clip1;
Clip clip2;
Clip clip3;
Clip clip4;
Clip clip5;
Clip clip6;
Clip clip7;


public void loadAudio()
{
    try
    {
        sample1 = AudioSystem.getAudioInputStream(getURL(soundFile1));
        clip1 = AudioSystem.getClip();
        clip1.open(sample1);

        sample2 = AudioSystem.getAudioInputStream(getURL(soundFile2));
        clip2 = AudioSystem.getClip();
        clip2.open(sample2);

        sample3 = AudioSystem.getAudioInputStream(getURL(soundFile3));
        clip3 = AudioSystem.getClip();
        clip3.open(sample3);

        sample4 = AudioSystem.getAudioInputStream(getURL(soundFile4));
        clip4 = AudioSystem.getClip();
        clip4.open(sample4);

        sample5 = AudioSystem.getAudioInputStream(getURL(soundFile5));
        clip5 = AudioSystem.getClip();
        clip5.open(sample5);

        sample6 = AudioSystem.getAudioInputStream(getURL(soundFile6));
        clip6 = AudioSystem.getClip();
        clip6.open(sample6);

        sample7 = AudioSystem.getAudioInputStream(getURL(soundFile7));
        clip7 = AudioSystem.getClip();
        clip7.open(sample7);
    }

    catch (MalformedURLException e) { }
    catch (IOException e) { }
    catch (LineUnavailableException e) { }
    catch (UnsupportedAudioFileException e) { }
    catch (Exception e) { }

}


private URL getURL(String filename)
{
    URL url = null;
    try
    {
        url = this.getClass().getResource(filename);
    }
    catch (Exception e) {}
    return url;
}

//personal method I made to stop all sounds from playing, otherwise clips would just play over each other which was extremely undesirable.
public void stopAll()
{
    if (clip1.isRunning() || clip2.isRunning() || clip3.isRunning() || clip4.isRunning() || clip5.isRunning() || clip6.isRunning() || clip7.isRunning() )
    {
        clip1.stop();
        clip1.setFramePosition(0);
        clip2.stop();
        clip2.setFramePosition(0);
        clip3.stop();
        clip3.setFramePosition(0);
        clip4.stop();
        clip4.setFramePosition(0);
        clip5.stop();
        clip5.setFramePosition(0);
        clip6.stop();
        clip6.setFramePosition(0);
        clip7.stop();
        clip7.setFramePosition(0);
    }
}

//f1 logic
public void f1()
{
        fKeyOne.x = -9999;
        fKeyOne.y = -9999;
        down1Off.x = 370;
        down1Off.y = 195;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip1.start();

        if(crazyCounter == 15)
        {
            removeRed();
            removeTwo();
            placeRedTwo();

            crazyCounter = 0;
        }
    }
}

//f2 logic
public void f2()
{
        fKeyTwo.x = -9999;
        fKeyTwo.y = -9999;
        down2Off.x = 420;
        down2Off.y = 195;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip2.start();

        if(crazyCounter == 0)
        {
            removeRed();
            removeTwo();
            placeRedTwo();

            crazyCounter++;

        }

        if(crazyCounter == 1)
        {
            removeRed();
            removeSix();
            placeRedSix();

            crazyCounter++;

        }

        if(crazyCounter == 3)
        {
            removeRed();
            removeSeven();
            placeRedSeven();

            crazyCounter++;

        }

        if(crazyCounter == 5)
        {
            removeRed();
            removeSix();
            placeRedSix();

            crazyCounter++;

        }

        if(crazyCounter == 7)
        {
            removeRed();
            removeFive();
            placeRedFive();

            crazyCounter++;
        }
    }
}


public void f3()
{
        fKeyThree.x = -9999;
        fKeyThree.y = -9999;
        down3Off.x = 470;
        down3Off.y = 195;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip3.start();

        if(crazyCounter == 10)
        {
            removeRed();
            removeFour();
            placeRedFour();

            crazyCounter++;
        }

        if(crazyCounter == 14)
        {
            removeRed();
            removeOne();
            placeRedOne();

            crazyCounter++;

        }
    }
}


public void f4()
{
        fKeyFour.x = -9999;
        fKeyFour.y = -9999;
        down4Off.x = 520;
        down4Off.y = 195;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip4.start();

        if(crazyCounter == 9)
        {
            removeRed();
            removeThree();
            placeRedThree();

            crazyCounter++;
        }

        if(crazyCounter == 11)
        {
            removeRed();
            removeFive();
            placeRedFive();

            crazyCounter++;
        }

        if(crazyCounter == 13)
        {
            removeRed();
            removeThree();
            placeRedThree();

            crazyCounter++;

        }
    }
}

public void f5()
{
        fKeyFive.x = -9999;
        fKeyFive.y = -9999;
        down5Off.x = 370;
        down5Off.y = 245;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip5.start();

        if(crazyCounter == 8)
        {
            removeRed();
            removeFour();
            placeRedFour();

            crazyCounter++;

        }

        if(crazyCounter == 12)
        {
            removeRed();
            removeFour();
            placeRedFour();

            crazyCounter++;
        }
    }
}


public void f6()
{
        fKeySix.x = -9999;
        fKeySix.y = -9999;
        down6Off.x = 420;
        down6Off.y = 245;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip6.start();

        if(crazyCounter == 2)
        {
            removeRed();
            removeTwo();
            placeRedTwo();

            crazyCounter++;

        }

        if(crazyCounter == 6)
        {
            removeRed();
            removeTwo();
            placeRedTwo();

            crazyCounter++;

        }
    }
}

public void f7()
{
        fKeySeven.x = -9999;
        fKeySeven.y = -9999;
        down7Off.x = 470;
        down7Off.y = 245;

    if(songArrowPos.y == crazyArrow.y)
    {
        stopAll();
        clip7.start();

        if(crazyCounter == 4)
        {
            removeRed();
            removeTwo();
            placeRedTwo();

            crazyCounter++;
        }
    }
}

public void f8()
{
        fKeyEight.x = -9999;
        fKeyEight.y = -9999;
        down8Off.x = 520;
        down8Off.y = 245;
}

}

BroHymn课程:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;



public class BroHymn extends Logic
{
String soundFile1 = "sounds/BRO1.wav";
String soundFile2 = "sounds/BRO2.wav";
String soundFile3 = "sounds/BRO3.wav";
String soundFile4 = "sounds/BRO4.wav";

AudioInputStream sample1;
AudioInputStream sample2;
AudioInputStream sample3;
AudioInputStream sample4;

一切都在加载。就像我的旧版本,第一首歌甚至播放,但没有任何更新。图形不会改变,歌曲不会改变。这就是为什么我想知道变量是否在类中没有被改变。我只是想解决这个问题。我道歉。

2 个答案:

答案 0 :(得分:1)

如果你有类似的话:

class MyClass {
 private int myValue = 0;
 public void setMyValue(int newValue) { myValue = newValue; }
}

每次实例化(即创建)类时,都可以设置MyClass的“myValue”变量:

MyClass Zero = new MyClass();

MyClass One = new MyClass();
One.setMyValue(1);

MyClass Two = new MyClass();
Two.setMyValue(2)

对于上面你现在有三个MyClass对象。第一个的myValue为0(默认值)。第二个的值为1,第三个的值为2。

答案 1 :(得分:0)

类的变量和对象的变量在Java中是不同的东西。对象变量是类的所有实例都可以访问的对象,每个实例都有自己的变量值。例如:

class A {
    int variable;
}

A的每个实例都可以将自己的值分配给variable

相比之下,类变量也存在。这些变量不是特定实例的一部分,而是类的一部分。这意味着所有实例共享此变量的相同值。如果更改,则所有实例都将看到此更改。也可以使用类的名称而不是实例的名称来访问该变量。在Java中,类变量是通过将它们声明为static来创建的,例如:

class B {
    static int variable;
}

现在B的每个实例都会为variable共享相同的值,并且每个实例都不可能拥有自己的值。

您声明int whatever = 3;的示例将介绍如何创建对象。为类中的变量提供初始值设定项时,所有对象在首次创建变量时都将具有该值。但是,值可以在创建后更改。它相当于将初始化器放在构造函数中。

因此,除非您将变量声明为final,否则在更改它们的值时应该没有问题。